Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose specialArgs.modulesPath and specialArgs.extraModulesPath to flake module consumers #303

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ jobs:
name: numtide
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: nix flake check
- run: nix develop -c echo OK
- name: Run devshell entry sanity checks
run: |
nix develop -c echo OK
for tmpl in ./templates/*; do
if ! [ -d "$tmpl" ]; then
continue
fi
nix develop --override-input devshell . "$tmpl" -c echo OK
done
- name: Run nix flake archive
run: nix flake archive
docs:
Expand Down
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ let
in
rec {
# Folder that contains all the extra modules
extraModulesDir = toString ./extra;
extraModulesPath = toString ./extra;

# Get the modules documentation from an empty evaluation
modules-docs = (eval {
configuration = {
# Load all of the extra modules so they appear in the docs
imports = importTree extraModulesDir;
imports = importTree extraModulesPath;
};
}).config.modules-docs;

Expand Down
4 changes: 3 additions & 1 deletion docs/src/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ lang = "en_US.UTF-8"
From a nix flake you would import it like

```nix
imports = ["${devshell}/extra/locale.nix"];
devshell.mkShell ({ extraModulesPath, ... }: {
imports = ["${extraModulesPath}/extra/locale.nix"];
})
```

## Building your own modules
Expand Down
4 changes: 1 addition & 3 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ in
Used to define devshells. not to be confused with `devShells`
'';

type = types.lazyAttrsOf (types.submoduleWith {
modules = import ./modules/modules.nix { inherit pkgs lib; };
});
type = types.lazyAttrsOf (types.submoduleWith (import ./modules/eval-args.nix { inherit pkgs lib; }));
default = { };
};
config.devShells = lib.mapAttrs (_name: devshell: devshell.devshell.shell) config.devshells;
Expand Down
15 changes: 4 additions & 11 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ nixpkgs:
, extraSpecialArgs ? { }
}:
let
devenvModules = import ./modules.nix {
module = lib.evalModules (import ./eval-args.nix {
inherit lib extraSpecialArgs;
pkgs = nixpkgs;
inherit lib;
};

module = lib.evalModules {
modules = [ configuration ] ++ devenvModules;
specialArgs = {
modulesPath = builtins.toString ./.;
extraModulesPath = builtins.toString ../extra;
} // extraSpecialArgs;
};
modules = [ configuration ];
});
in
{
inherit (module) config options;
Expand Down
18 changes: 18 additions & 0 deletions modules/eval-args.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Arguments for `lib.evalModules` or `types.submoduleWith`.
{ pkgs
, lib
, modules ? [ ]
, extraSpecialArgs ? { }
}:
let
devenvModules = import ./modules.nix {
inherit lib pkgs;
};
in
{
modules = (lib.toList modules) ++ devenvModules;
specialArgs = {
modulesPath = builtins.toString ./.;
extraModulesPath = builtins.toString ../extra;
} // extraSpecialArgs;
}
6 changes: 3 additions & 3 deletions nix/importTOML.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ let
dir = toString (builtins.dirOf file);
data = builtins.fromTOML (builtins.readFile file);

extraModulesDir = toString ../extra;
extraModules = builtins.readDir extraModulesDir;
extraModulesPath = toString ../extra;
extraModules = builtins.readDir extraModulesPath;

importModule = str:
let
repoFile = "${dir}/${str}";
extraFile =
"${extraModulesDir}/${builtins.replaceStrings [ "." ] [ "/" ] str}.nix";
"${extraModulesPath}/${builtins.replaceStrings [ "." ] [ "/" ] str}.nix";
in
# First try to import from the user's repository
if lib.hasPrefix "./" str || lib.hasSuffix ".nix" str || lib.hasSuffix ".toml" str
Expand Down
19 changes: 18 additions & 1 deletion templates/flake-parts/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@
];

perSystem = { pkgs, ... }: {
devshells.default = { };
devshells.default = ({ extraModulesPath, ... } @ args: {
# `extraModulesPath` provides access to additional modules that are
# not included in the standard devshell modules list.
#
# Please see https://numtide.github.io/devshell/extending.html for
# documentation on consuming extra modules, and see
# https://github.com/numtide/devshell/tree/main/extra for the
# extra modules that are currently available.
imports = [
"${extraModulesPath}/git/hooks.nix"
];

git.hooks.enable = false;
git.hooks.pre-commit.text = ''
echo 1>&2 'time to implement a pre-commit hook!'
exit 1
'';
});
};
};
}
Loading