From fda4e7530c5399d84987d8f18dfc66bf5862886e Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Tue, 15 Jul 2025 18:50:56 +0200 Subject: [PATCH] Flake & cabal setup --- .ghci | 1 + .gitignore | 6 +++ LICENSE | 7 ++++ README.md | 0 app/Main.hs | 18 ++++++++ flake.lock | 107 +++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 80 +++++++++++++++++++++++++++++++++++ hie.yaml | 7 ++++ src/Yore/DB.hs | 21 ++++++++++ src/Yore/Repl.hs | 13 ++++++ yore.cabal | 64 ++++++++++++++++++++++++++++ 11 files changed, 324 insertions(+) create mode 100644 .ghci create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 app/Main.hs create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 hie.yaml create mode 100644 src/Yore/DB.hs create mode 100644 src/Yore/Repl.hs create mode 100644 yore.cabal diff --git a/.ghci b/.ghci new file mode 100644 index 0000000..97d2527 --- /dev/null +++ b/.ghci @@ -0,0 +1 @@ +import Yore.Repl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ae1d06 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.stack-work/ +dist-newstyle +.vscode/ +*.swp +result +bench.html diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b7a7c88 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2025 Paul Brinkmeier + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..9aae159 --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main (main) where + +import Database.PostgreSQL.LibPQ (connectdb) + +import qualified Yore.DB + +main :: IO () +main = do + conn <- connectdb "host=localhost port=5432" + result <- Yore.DB.getTables conn + + case result of + Left e -> + putStrLn $ "Got error: " ++ show e + Right rows -> + mapM_ print rows diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3e2aefa --- /dev/null +++ b/flake.lock @@ -0,0 +1,107 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1752536923, + "narHash": "sha256-fdgPZR7VFSSRIQKOJLcs3qCJBWM64Uak0gAGtWTYAd8=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "c665e4d918eda5d78a175ed8d300809c44932160", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "release-25.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "opium": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1752596794, + "narHash": "sha256-1J3tNaosj3DCQuzDCPdbEbOd5ZQuqIOAVD7o5hFNx+g=", + "ref": "main", + "rev": "3879c2603fa124087a3ed810903bf75d38a27598", + "revCount": 56, + "type": "git", + "url": "https://git.pbrinkmeier.de/paul/opium" + }, + "original": { + "ref": "main", + "type": "git", + "url": "https://git.pbrinkmeier.de/paul/opium" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs", + "opium": "opium" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..9d08b9f --- /dev/null +++ b/flake.nix @@ -0,0 +1,80 @@ +{ + description = "Le olde newspaper display"; + + inputs.nixpkgs.url = "github:nixos/nixpkgs/release-25.05"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.gitignore = { + url = "github:hercules-ci/gitignore.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + inputs.opium = { + url = "git+https://git.pbrinkmeier.de/paul/opium?ref=main"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-utils.follows = "flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils, gitignore, opium }: + flake-utils.lib.eachSystem ["x86_64-linux" "aarch64-linux" "aarch64-darwin"] (system: + let + pkgs = import nixpkgs { inherit system; }; + + inherit (pkgs.haskell.lib) compose; + + # Include opium in package set + addOpium = self: super: { + opium = opium.packages.${system}.opium; + }; + + # For building the package, we use only the files not ignored by Git as inputs. + # Also, flake.nix and flake.lock are not included to avoid rebuilds when + # working on them. + src = pkgs.lib.cleanSourceWith { + src = gitignore.lib.gitignoreSource ./.; + filter = path: type: builtins.elem (builtins.baseNameOf path) [ "flake.nix" "flake.lock" ]; + }; + + yore = pkgs.lib.pipe + # The basic package + (pkgs.haskellPackages.developPackage { + root = ./.; + overrides = addOpium; + }) + # And some build configuration. + # See https://nixos.org/manual/nixpkgs/unstable/#haskell-packaging-helpers. + [ + # Remove warp which pulls GHC into the runtime deps for some reason. + (compose.overrideCabal (drv: { + postInstall = '' + remove-references-to -t ${pkgs.haskellPackages.warp} $out/bin/yore + ''; + })) + # Return only the bin folder to curb image size + compose.justStaticExecutables + ]; + in { + packages = rec { + docker = + pkgs.dockerTools.buildImage { + name = "git.pbrinkmeier.de/paul/yore"; + tag = yore.version; + config.Cmd = [ "${yore}/bin/yore" ]; + }; + + default = yore; + + opium_ = opium.packages.${system}.opium; + }; + + devShells.default = + (pkgs.haskellPackages.developPackage { + root = ./.; + modifier = drv: + pkgs.haskell.lib.addBuildTools drv [ + pkgs.cabal-install + pkgs.haskellPackages.implicit-hie + pkgs.haskell-language-server + ]; + overrides = addOpium; + }).env; + }); +} diff --git a/hie.yaml b/hie.yaml new file mode 100644 index 0000000..0ab237a --- /dev/null +++ b/hie.yaml @@ -0,0 +1,7 @@ +cradle: + cabal: + - path: "src" + component: "lib:yore" + + - path: "app/Main.hs" + component: "yore:exe:yore" diff --git a/src/Yore/DB.hs b/src/Yore/DB.hs new file mode 100644 index 0000000..f11c253 --- /dev/null +++ b/src/Yore/DB.hs @@ -0,0 +1,21 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Yore.DB + ( Table (..) + , getTables +) where + +import Database.PostgreSQL.LibPQ (Connection) +import GHC.Generics (Generic) + +import qualified Database.PostgreSQL.Opium as Opium + +data Table = Table + { schema :: String + , name :: String + } deriving (Show, Generic) + +instance Opium.FromRow Table + +getTables :: Connection -> IO (Either Opium.Error [Table]) +getTables = Opium.fetch_ "SELECT table_schema AS schema, table_name AS name FROM information_schema.tables" diff --git a/src/Yore/Repl.hs b/src/Yore/Repl.hs new file mode 100644 index 0000000..52c05d9 --- /dev/null +++ b/src/Yore/Repl.hs @@ -0,0 +1,13 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Yore.Repl (connect, exec) where + +import qualified Database.PostgreSQL.LibPQ as LibPQ + +import Database.PostgreSQL.LibPQ (Connection) + +connect :: IO Connection +connect = LibPQ.connectdb "host=localhost" + +exec :: (Connection -> IO a) -> IO a +exec f = f =<< connect diff --git a/yore.cabal b/yore.cabal new file mode 100644 index 0000000..d40ffca --- /dev/null +++ b/yore.cabal @@ -0,0 +1,64 @@ +cabal-version: 3.4 + +name: yore +version: 0 +author: Paul Brinkmeier +maintainer: hallo@pbrinkmeier.de +copyright: 2023 Paul Brinkmeier +license: MIT +license-file: LICENSE +build-type: Simple +extra-source-files: + README.md + +source-repository head + type: git + location: https://git.pbrinkmeier.de/paul/yore + +common shared-options + ghc-options: + -Wall + -Wcompat + -Widentities + -Wincomplete-record-updates + -Wincomplete-uni-patterns + -Wmissing-export-lists + -Wmissing-home-modules + -Wpartial-fields + -Wredundant-constraints + default-language: GHC2021 + +library + import: shared-options + exposed-modules: + Yore.DB + , Yore.Repl + hs-source-dirs: + src + build-depends: + base >=4.18 && <5 + , opium + , postgresql-libpq + +executable yore + import: shared-options + main-is: Main.hs + hs-source-dirs: + app + ghc-options: + -threaded + -rtsopts + -with-rtsopts=-N + -ghci-script=.ghci + build-depends: + yore + , base >=4.18 && <5 + , blaze-html + , bytestring + , http-media + , opium + , postgresql-libpq + , servant-server + , text + , wai + , warp