Skip to content

Commit 7c9e793

Browse files
authored
Merge pull request #303 from tomeon/flake-module-special-args
Expose `specialArgs.modulesPath` and `specialArgs.extraModulesPath` to flake module consumers
2 parents f7795ed + 4e4fd3b commit 7c9e793

File tree

8 files changed

+64
-21
lines changed

8 files changed

+64
-21
lines changed

.github/workflows/nix.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ jobs:
4141
name: numtide
4242
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
4343
- run: nix flake check
44-
- run: nix develop -c echo OK
44+
- name: Run devshell entry sanity checks
45+
run: |
46+
nix develop -c echo OK
47+
for tmpl in ./templates/*; do
48+
if ! [ -d "$tmpl" ]; then
49+
continue
50+
fi
51+
nix develop --override-input devshell . "$tmpl" -c echo OK
52+
done
4553
- name: Run nix flake archive
4654
run: nix flake archive
4755
docs:

default.nix

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ let
3333
in
3434
rec {
3535
# Folder that contains all the extra modules
36-
extraModulesDir = toString ./extra;
36+
extraModulesPath = toString ./extra;
37+
38+
# Alias for backward compatibility.
39+
extraModulesDir = extraModulesPath;
3740

3841
# Get the modules documentation from an empty evaluation
3942
modules-docs =
4043
(eval {
4144
configuration = {
4245
# Load all of the extra modules so they appear in the docs
43-
imports = importTree extraModulesDir;
46+
imports = importTree extraModulesPath;
4447
};
4548
}).config.modules-docs;
4649

docs/src/extending.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ lang = "en_US.UTF-8"
3131
From a nix flake you would import it like
3232

3333
```nix
34-
imports = ["${devshell}/extra/locale.nix"];
34+
devshell.mkShell ({ extraModulesPath, ... }: {
35+
imports = ["${extraModulesPath}/locale.nix"];
36+
})
3537
```
3638

3739
## Building your own modules

flake-module.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ in
2121
'';
2222

2323
type = types.lazyAttrsOf (
24-
types.submoduleWith { modules = import ./modules/modules.nix { inherit pkgs lib; }; }
24+
types.submoduleWith (import ./modules/eval-args.nix { inherit pkgs lib; })
2525
);
2626
default = { };
2727
};

modules/default.nix

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ nixpkgs:
66
extraSpecialArgs ? { },
77
}:
88
let
9-
devenvModules = import ./modules.nix {
10-
pkgs = nixpkgs;
11-
inherit lib;
12-
};
13-
14-
module = lib.evalModules {
15-
modules = [ configuration ] ++ devenvModules;
16-
specialArgs = {
17-
modulesPath = builtins.toString ./.;
18-
extraModulesPath = builtins.toString ../extra;
19-
} // extraSpecialArgs;
20-
};
9+
module = lib.evalModules (
10+
import ./eval-args.nix {
11+
inherit lib extraSpecialArgs;
12+
pkgs = nixpkgs;
13+
modules = [ configuration ];
14+
}
15+
);
2116
in
2217
{
2318
inherit (module) config options;

modules/eval-args.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Arguments for `lib.evalModules` or `types.submoduleWith`.
2+
{
3+
pkgs,
4+
lib,
5+
modules ? [ ],
6+
extraSpecialArgs ? { },
7+
}:
8+
let
9+
devenvModules = import ./modules.nix { inherit lib pkgs; };
10+
in
11+
{
12+
modules = (lib.toList modules) ++ devenvModules;
13+
specialArgs = {
14+
modulesPath = builtins.toString ./.;
15+
extraModulesPath = builtins.toString ../extra;
16+
} // extraSpecialArgs;
17+
}

nix/importTOML.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ let
77
dir = toString (builtins.dirOf file);
88
data = builtins.fromTOML (builtins.readFile file);
99

10-
extraModulesDir = toString ../extra;
11-
extraModules = builtins.readDir extraModulesDir;
10+
extraModulesPath = toString ../extra;
11+
extraModules = builtins.readDir extraModulesPath;
1212

1313
importModule =
1414
str:
1515
let
1616
repoFile = "${dir}/${str}";
17-
extraFile = "${extraModulesDir}/${builtins.replaceStrings [ "." ] [ "/" ] str}.nix";
17+
extraFile = "${extraModulesPath}/${builtins.replaceStrings [ "." ] [ "/" ] str}.nix";
1818
in
1919
# First try to import from the user's repository
2020
if lib.hasPrefix "./" str || lib.hasSuffix ".nix" str || lib.hasSuffix ".toml" str then

templates/flake-parts/flake.nix

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,25 @@
2525
perSystem =
2626
{ pkgs, ... }:
2727
{
28-
devshells.default = { };
28+
devshells.default = (
29+
{ extraModulesPath, ... }@args:
30+
{
31+
# `extraModulesPath` provides access to additional modules that are
32+
# not included in the standard devshell modules list.
33+
#
34+
# Please see https://numtide.github.io/devshell/extending.html for
35+
# documentation on consuming extra modules, and see
36+
# https://github.com/numtide/devshell/tree/main/extra for the
37+
# extra modules that are currently available.
38+
imports = [ "${extraModulesPath}/git/hooks.nix" ];
39+
40+
git.hooks.enable = false;
41+
git.hooks.pre-commit.text = ''
42+
echo 1>&2 'time to implement a pre-commit hook!'
43+
exit 1
44+
'';
45+
}
46+
);
2947
};
3048
};
3149
}

0 commit comments

Comments
 (0)