From 67d2e0c75334731ef6f65d0dc49ad2237a144c7b Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Thu, 23 Feb 2023 16:44:05 -0500 Subject: [PATCH] Add `devShell.mkShellArgs` (#92) --- CHANGELOG.md | 1 + flake-module.nix | 15 +++++++++++++++ haskell-project.nix | 8 +++++--- test/flake.nix | 12 +++++++++--- test/test.sh | 8 ++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f1368f..b99e0f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - #63: Add `config.haskellProjects.${name}.outputs` containing all flake outputs for that project. - #49 & #91: The `packages` option now autodiscovers the top-level `.cabal` file (in addition to looking inside sub-directories) as its default value. - #69: The default flake template creates `flake.nix` only, while the `#example` one creates the full Haskell project template. + - #92: Add `devShell.mkShellArgs` to pass custom arguments to `mkShell` - API changes - #37: Group `buildTools` (renamed to `tools`), `hlsCheck` and `hlintCheck` under the `devShell` submodule option; and allow disabling them all using `devShell.enable = false;` (useful if you want haskell-flake to produce just the package outputs). - #64: Remove hlintCheck (use [treefmt-nix](https://github.com/numtide/treefmt-nix#flake-parts) instead) diff --git a/flake-module.nix b/flake-module.nix index e3b5e0a..85383a3 100644 --- a/flake-module.nix +++ b/flake-module.nix @@ -69,6 +69,21 @@ in A [check](flake-parts.html#opt-perSystem.checks) to make sure that your IDE will work. ''; }; + mkShellArgs = mkOption { + type = types.attrsOf types.raw; + description = '' + Extra arguments to pass to `pkgs.mkShell`. + ''; + default = { }; + example = '' + { + shellHook = \'\' + # Re-generate .cabal files so HLS will work (per hie.yaml) + ''${pkgs.findutils}/bin/find -name package.yaml -exec hpack {} \; + \'\'; + }; + ''; + }; }; }; projectSubmodule = types.submoduleWith { diff --git a/haskell-project.nix b/haskell-project.nix index 8437529..a27c2a8 100644 --- a/haskell-project.nix +++ b/haskell-project.nix @@ -74,14 +74,16 @@ in hlint; }; nativeBuildInputs = lib.attrValues (defaultBuildTools finalPackages // config.devShell.tools finalPackages); - devShell = finalPackages.shellFor { - inherit nativeBuildInputs; + mkShellArgs = config.devShell.mkShellArgs // { + nativeBuildInputs = (config.devShell.mkShellArgs.nativeBuildInputs or [ ]) ++ nativeBuildInputs; + }; + devShell = finalPackages.shellFor (mkShellArgs // { packages = p: map (name: p."${name}") (lib.attrNames config.packages); withHoogle = true; - }; + }); in { finalPackages = config.basePackages.extend config.finalOverlay; diff --git a/test/flake.nix b/test/flake.nix index 7447162..0c19a47 100644 --- a/test/flake.nix +++ b/test/flake.nix @@ -46,9 +46,15 @@ # docs. foo = self.callCabal2nix "foo" (inputs.haskell-multi-nix + /foo) { }; }; - devShell.tools = hp: { - # Adding a tool should make it available in devshell. - fzf = pkgs.fzf; + devShell = { + tools = hp: { + # Adding a tool should make it available in devshell. + fzf = pkgs.fzf; + }; + mkShellArgs.shellHook = '' + echo "Hello from devshell!" + export FOO=bar + ''; }; }; # haskell-flake doesn't set the default package, but you can do it here. diff --git a/test/test.sh b/test/test.sh index 6d26eed..621a1fc 100755 --- a/test/test.sh +++ b/test/test.sh @@ -16,3 +16,11 @@ which ghcid && exit 2 || echo # Adding a buildTool (fzf, here) should put it in devshell. which fzf + +# mkShellArgs works +if [[ "$FOO" == "bar" ]]; then + echo "$FOO" +else + echo "FOO is not bar" + exit 2 +fi \ No newline at end of file