Skip to content

Commit

Permalink
Reallow custom postgres extensions like postgis in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Oct 11, 2024
1 parent b27260d commit a5bcb83
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
60 changes: 33 additions & 27 deletions Guide/package-management.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -371,47 +371,53 @@ After that try to run `devenv up`.

### Building Postgres With Extensions

**TODO: Fix this for IHP v1.1.0**

For some applications you may want to install custom postgres extension
libraries and have them available in the nix store.

For example to enable the [postgis](https://postgis.net/) spatial
and geographic objects in PostgreSQL add
`postgresExtensions = (p: [ p.postgis ]);` to your project's `default.nix` file as
an attribute of the `"{ihp}/NixSupport/default.nix"` expression.
`services.postgres.extensions = extensions: [ extensions.postgis ];` to your project's `flake.nix`:


```nix
let
ihp = builtins.fetchGit {
url = "https://github.com/digitallyinduced/ihp.git";
rev = "c6d40612697bb7905802f23b7753702d33b9e2c1";
};
haskellEnv = import "${ihp}/NixSupport/default.nix" {
ihp = ihp;
postgresExtensions = (p: [ p.postgis ]);
haskellDeps = p: with p; [
cabal-install
base
wai
text
hlint
p.ihp
google-oauth2
];
otherDeps = p: with p; [
];
projectPath = ./.;
{
inputs = {
ihp.url = "path:///Users/marc/digitallyinduced/ihp";
ihp.inputs.nixpkgs.url = "github:mpscholten/nixpkgs/fix-th-desugar";
nixpkgs.follows = "ihp/nixpkgs";
flake-parts.follows = "ihp/flake-parts";
devenv.follows = "ihp/devenv";
systems.follows = "ihp/systems";
};
in
haskellEnv
outputs = inputs@{ ihp, flake-parts, systems, nixpkgs, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = import systems;
imports = [ ihp.flakeModules.default ];
perSystem = { pkgs, ... }: {
ihp = {
inherit appName;
enable = true;
projectPath = ./.;
packages = with pkgs; [];
haskellPackages = p: with p; [
# ...
];
};
devenv.shells.default = {
services.postgres.extensions = extensions: [ extensions.postgis ];
};
};
};
}
```

Behind the scenes this will pass a function to the postgres nix expressions `postgresql.withPackages`
function making the extension in your app's nix store postgres package.

After the install you can run `create extension postgis;` to enable all the features of the
After the install you can run `CREATE EXTENSION postgis;` to enable all the features of the
installed extension.

### Stopping Nix From Running Tests for a Haskell Dependency
Expand Down
3 changes: 0 additions & 3 deletions NixSupport/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
, otherDeps ? (p: [])
, projectPath ? ./.
, withHoogle ? false
, postgresExtensions ? (p: [])
, optimized ? false
, includeDevTools ? !optimized # Include Postgres?
, rtsFlags ? ""
, appName ? "app"
, optimizationLevel ? "2"
Expand Down Expand Up @@ -175,7 +173,6 @@ in
[ pkgs.makeWrapper
pkgs.cacert # Needed for npm install to work from within the IHP build process
]
(if includeDevTools then [(pkgs.postgresql_13.withPackages postgresExtensions)] else [])
];
shellHook = "eval $(egrep ^export ${allHaskellPackages}/bin/ghc)";
enableParallelBuilding = true;
Expand Down
18 changes: 14 additions & 4 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ ihpFlake:
haskellDeps = cfg.haskellPackages;
otherDeps = p: cfg.packages;
projectPath = cfg.projectPath;
# Dev tools are not needed in the release build
includeDevTools = false;
# Set optimized = true to get more optimized binaries, but slower build times
optimized = true;
ghc = ghcCompiler;
Expand All @@ -156,7 +154,6 @@ ihpFlake:
haskellDeps = cfg.haskellPackages;
otherDeps = p: cfg.packages;
projectPath = cfg.projectPath;
includeDevTools = false;
optimized = false;
ghc = ghcCompiler;
pkgs = pkgs;
Expand Down Expand Up @@ -204,9 +201,21 @@ ihpFlake:
};

devenv.shells.default = lib.mkIf cfg.enable {
packages = [ ghcCompiler.ihp ghcCompiler.ihp-ide pkgs.postgresql_13 pkgs.gnumake ]
packages = [ ghcCompiler.ihp ghcCompiler.ihp-ide pkgs.gnumake ]
++ cfg.packages
++ [pkgs.mktemp] # Without this 'make build/bin/RunUnoptimizedProdServer' fails on macOS
++ [(let cfg = config.devenv.shells.default.services.postgres; in
if cfg.extensions != null
then
if builtins.hasAttr "withPackages" cfg.package
then cfg.package.withPackages cfg.extensions
else
builtins.throw ''
Cannot add extensions to the PostgreSQL package.
`services.postgres.package` is missing the `withPackages` attribute. Did you already add extensions to the package?
''
else cfg.package
)]
;

/*
Expand Down Expand Up @@ -239,6 +248,7 @@ ihpFlake:
# As the devenv postgres uses a different location for the socket
# this would break lots of known commands such as `make db`
services.postgres.enable = false;
services.postgres.package = pkgs.postgresql_13;
services.postgres.initialDatabases = [
{
name = "app";
Expand Down

0 comments on commit a5bcb83

Please sign in to comment.