Skip to content

Commit 55de7d4

Browse files
authored
Merge pull request #48 from welteki/flake-modules
Support flake modules
2 parents 9a81009 + 16adfb4 commit 55de7d4

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Instead of `vm.nix`, `nixos-shell` also accepts other modules on the command lin
3131
$ nixos-shell some-nix-module.nix
3232
```
3333

34-
You can also start a vm from a flake's `nixosConfigurations` using the `--flake` flag.
34+
You can also start a vm from a flake's `nixosConfigurations` or `nixosModules` output using the `--flake` flag.
3535

3636
```console
3737
$ nixos-shell --flake github:Mic92/nixos-shell#vm-forward
@@ -52,6 +52,16 @@ This will run the `vm-forward` example.
5252
>}
5353
>```
5454
55+
When using the `--flake` flag, if no attribute is given, `nixos-shell` tries the following flake output attributes:
56+
- `packages.<system>.nixosConfigurations.<vm>`
57+
- `nixosConfigurations.<vm>`
58+
- `nixosModules.<vm>`
59+
60+
If an attribute _name_ is given, `nixos-shell` tries the following flake output attributes:
61+
- `packages.<system>.nixosConfigurations.<name>`
62+
- `nixosConfigurations.<name>`
63+
- `nixosModules.<name>`
64+
5565
## Terminating the virtual machine
5666
5767
Type `Ctrl-a x` to exit the virtual machine.

share/nixos-shell.nix

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,49 @@
66
, flakeAttr ? null
77
}:
88
let
9+
lib = (import nixpkgs { }).lib;
10+
911
nixos-shell = import ./modules/nixos-shell.nix;
1012
nixos-shell-config = import ./modules/nixos-shell-config.nix;
1113

12-
flake = builtins.getFlake flakeUri;
13-
flakeSystem = flake.outputs.packages."${system}".nixosConfigurations."${flakeAttr}" or flake.outputs.nixosConfigurations."${flakeAttr}";
14-
in
15-
if flakeUri != null then
16-
flakeSystem.override
17-
(attrs: {
18-
modules =
19-
let
20-
nixosShellModules =
21-
if flakeSystem ? options.nixos-shell then
22-
[ nixos-shell-config ]
23-
else
24-
[ nixos-shell nixos-shell-config ];
25-
in
26-
attrs.modules ++ nixosShellModules;
27-
})
28-
else
29-
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
14+
defaultTo = default: e: if e == null then default else e;
15+
16+
getFlakeOutput = path: lib.attrByPath path null flake.outputs;
17+
18+
mkShellSystem = config: import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
3019
inherit system;
3120
modules = [
32-
configuration
21+
config
3322
nixos-shell
3423
nixos-shell-config
3524
];
36-
}
25+
};
26+
27+
flake = builtins.getFlake flakeUri;
28+
29+
flakeSystem = defaultTo
30+
(getFlakeOutput [ "nixosConfigurations" "${flakeAttr}" ])
31+
(getFlakeOutput [ "packages" "${system}" "nixosConfigurations" "${flakeAttr}" ]);
32+
33+
flakeModule = getFlakeOutput [ "nixosModules" "${flakeAttr}" ];
34+
in
35+
if flakeUri != null then
36+
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+
})
49+
else if flakeModule != null then
50+
mkShellSystem flakeModule
51+
else
52+
throw "cannot find flake attribute '${flakeUri}#${flakeAttr}'"
53+
else
54+
mkShellSystem configuration

0 commit comments

Comments
 (0)