Skip to content

Commit 2cb7687

Browse files
authored
Merge pull request #58 from tomeon/support-extendModules
Support `extendModules` for injecting nixos-shell's options and config modules
2 parents 362f872 + 7006b36 commit 2cb7687

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ test-mounts:
2121
test-efi:
2222
$(NIXOS_SHELL) examples/vm-efi.nix
2323

24+
test-broken:
25+
! $(NIXOS_SHELL) --flake '.#BROKEN-DO-NOT-USE-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING'
26+
2427
install:
2528
$(INSTALL) -D bin/nixos-shell $(DESTDIR)$(PREFIX)/bin/nixos-shell
2629
$(INSTALL) -D share/modules/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/modules/nixos-shell.nix

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ $ nixos-shell --flake github:Mic92/nixos-shell#vm-forward
3939

4040
This will run the `vm-forward` example.
4141

42-
> Note: system configurations have to be made overridable with `lib.makeOverridable` to use them with `nixos-shell`
42+
> Note: `nixos-shell` must be able to extend the specified system configuration with [certain modules](share/modules).
43+
>
44+
> If your version of `nixpkgs` provides the `extendModules` function on system configurations, `nixos-shell` will use it to inject the required modules; no additional work on your part is needed.
45+
>
46+
> If your version of `nixpkgs` **does not** provide `extendModules`, you must make your system configurations overridable with `lib.makeOverridable` to use them with `nixos-shell`:
4347
>```nix
4448
>{
4549
> nixosConfigurations = let
@@ -51,6 +55,7 @@ This will run the `vm-forward` example.
5155
> };
5256
>}
5357
>```
58+
> Specifying a non-overridable system configuration will cause `nixos-shell` to abort with a non-zero exit status.
5459
5560
When using the `--flake` flag, if no attribute is given, `nixos-shell` tries the following flake output attributes:
5661
- `packages.<system>.nixosConfigurations.<vm>`

bin/nixos-shell

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ if [[ -z "$flake_uri" ]]; then
6565
)
6666
else
6767
extraBuildFlags+=(
68-
--extra-experimental-features "flakes"
68+
--extra-experimental-features "flakes"
69+
--argstr flakeStr "$flake"
6970
--argstr flakeUri "$flake_uri"
7071
--argstr flakeAttr "${flake_attr:-"vm"}"
7172
)

flake.nix

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@
1818

1919
mkSystem = pkgs: config: makeOverridable nixosSystem {
2020
system = "x86_64-linux";
21-
modules = [ config inp.self.nixosModules.nixos-shell ];
21+
modules = [ config inp.self.nixosModules.nixos-shell ];
2222
};
2323

2424
supportedSystems = [ "x86_64-linux" ];
2525
in
2626
{
27-
nixosConfigurations = mapAttrs (_name: config: mkSystem inp.nixpkgs config) vms;
27+
nixosConfigurations =
28+
let
29+
configs = mapAttrs (_name: config: mkSystem inp.nixpkgs config) vms;
30+
in
31+
configs
32+
//
33+
{
34+
# Used for testing that nixos-shell exits nonzero when provided a
35+
# non-extensible config
36+
BROKEN-DO-NOT-USE-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING =
37+
removeAttrs configs.vm [ "extendModules" "override" ];
38+
};
2839

2940
nixosModules.nixos-shell = import ./share/modules/nixos-shell.nix;
3041
}

share/nixos-shell.nix

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
, system ? builtins.currentSystem
33
, configuration ? <nixos-config>
44

5+
, flakeStr ? null # flake as named on the command line
56
, flakeUri ? null
67
, flakeAttr ? null
78
}:
@@ -31,21 +32,39 @@ let
3132
(getFlakeOutput [ "packages" "${system}" "nixosConfigurations" "${flakeAttr}" ]);
3233

3334
flakeModule = getFlakeOutput [ "nixosModules" "${flakeAttr}" ];
35+
36+
nixosShellModules =
37+
if flakeSystem ? options.nixos-shell then
38+
[ nixos-shell-config ]
39+
else
40+
[ nixos-shell nixos-shell-config ];
3441
in
3542
if flakeUri != null then
3643
if flakeSystem != null then
37-
flakeSystem.override
38-
(attrs: {
39-
modules =
40-
let
41-
nixosShellModules =
42-
if flakeSystem ? options.nixos-shell then
43-
[ nixos-shell-config ]
44-
else
45-
[ nixos-shell nixos-shell-config ];
46-
in
47-
attrs.modules ++ nixosShellModules;
48-
})
44+
if flakeSystem ? "extendModules" then
45+
flakeSystem.extendModules { modules = nixosShellModules; }
46+
else if flakeSystem ? "override" then
47+
flakeSystem.override (attrs: { modules = attrs.modules ++ nixosShellModules; })
48+
else
49+
throw ''
50+
'${flakeStr}#${flakeAttr}' is missing the expected 'override' attribute.
51+
52+
Please ensure that '${flakeStr}#${flakeAttr}' is an overridable attribute set by declaring it with 'lib.makeOverridable'.
53+
54+
For instance:
55+
56+
nixosConfigurations = let
57+
lib = nixpkgs.lib;
58+
in {
59+
"${flakeAttr}" = lib.makeOverridable lib.nixosSystem {
60+
# ...
61+
};
62+
};
63+
64+
Alternatively, upgrade to a version of nixpkgs that provides the 'extendModules' function on NixOS system configurations.
65+
66+
See https://github.com/Mic92/nixos-shell#start-a-virtual-machine for additional information.
67+
''
4968
else if flakeModule != null then
5069
mkShellSystem flakeModule
5170
else

0 commit comments

Comments
 (0)