Skip to content

Commit

Permalink
Merge pull request #58 from tomeon/support-extendModules
Browse files Browse the repository at this point in the history
Support `extendModules` for injecting nixos-shell's options and config modules
  • Loading branch information
Mic92 authored May 2, 2023
2 parents 362f872 + 7006b36 commit 2cb7687
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ test-mounts:
test-efi:
$(NIXOS_SHELL) examples/vm-efi.nix

test-broken:
! $(NIXOS_SHELL) --flake '.#BROKEN-DO-NOT-USE-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING'

install:
$(INSTALL) -D bin/nixos-shell $(DESTDIR)$(PREFIX)/bin/nixos-shell
$(INSTALL) -D share/modules/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/modules/nixos-shell.nix
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ $ nixos-shell --flake github:Mic92/nixos-shell#vm-forward

This will run the `vm-forward` example.

> Note: system configurations have to be made overridable with `lib.makeOverridable` to use them with `nixos-shell`
> Note: `nixos-shell` must be able to extend the specified system configuration with [certain modules](share/modules).
>
> 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.
>
> 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`:
>```nix
>{
> nixosConfigurations = let
Expand All @@ -51,6 +55,7 @@ This will run the `vm-forward` example.
> };
>}
>```
> Specifying a non-overridable system configuration will cause `nixos-shell` to abort with a non-zero exit status.
When using the `--flake` flag, if no attribute is given, `nixos-shell` tries the following flake output attributes:
- `packages.<system>.nixosConfigurations.<vm>`
Expand Down
3 changes: 2 additions & 1 deletion bin/nixos-shell
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ if [[ -z "$flake_uri" ]]; then
)
else
extraBuildFlags+=(
--extra-experimental-features "flakes"
--extra-experimental-features "flakes"
--argstr flakeStr "$flake"
--argstr flakeUri "$flake_uri"
--argstr flakeAttr "${flake_attr:-"vm"}"
)
Expand Down
15 changes: 13 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@

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

supportedSystems = [ "x86_64-linux" ];
in
{
nixosConfigurations = mapAttrs (_name: config: mkSystem inp.nixpkgs config) vms;
nixosConfigurations =
let
configs = mapAttrs (_name: config: mkSystem inp.nixpkgs config) vms;
in
configs
//
{
# Used for testing that nixos-shell exits nonzero when provided a
# non-extensible config
BROKEN-DO-NOT-USE-UNLESS-YOU-KNOW-WHAT-YOU-ARE-DOING =
removeAttrs configs.vm [ "extendModules" "override" ];
};

nixosModules.nixos-shell = import ./share/modules/nixos-shell.nix;
}
Expand Down
43 changes: 31 additions & 12 deletions share/nixos-shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, system ? builtins.currentSystem
, configuration ? <nixos-config>

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

flakeModule = getFlakeOutput [ "nixosModules" "${flakeAttr}" ];

nixosShellModules =
if flakeSystem ? options.nixos-shell then
[ nixos-shell-config ]
else
[ nixos-shell nixos-shell-config ];
in
if flakeUri != null then
if flakeSystem != null then
flakeSystem.override
(attrs: {
modules =
let
nixosShellModules =
if flakeSystem ? options.nixos-shell then
[ nixos-shell-config ]
else
[ nixos-shell nixos-shell-config ];
in
attrs.modules ++ nixosShellModules;
})
if flakeSystem ? "extendModules" then
flakeSystem.extendModules { modules = nixosShellModules; }
else if flakeSystem ? "override" then
flakeSystem.override (attrs: { modules = attrs.modules ++ nixosShellModules; })
else
throw ''
'${flakeStr}#${flakeAttr}' is missing the expected 'override' attribute.
Please ensure that '${flakeStr}#${flakeAttr}' is an overridable attribute set by declaring it with 'lib.makeOverridable'.
For instance:
nixosConfigurations = let
lib = nixpkgs.lib;
in {
"${flakeAttr}" = lib.makeOverridable lib.nixosSystem {
# ...
};
};
Alternatively, upgrade to a version of nixpkgs that provides the 'extendModules' function on NixOS system configurations.
See https://github.com/Mic92/nixos-shell#start-a-virtual-machine for additional information.
''
else if flakeModule != null then
mkShellSystem flakeModule
else
Expand Down

0 comments on commit 2cb7687

Please sign in to comment.