Skip to content

Commit 21fc93a

Browse files
committed
split nixos-shell module
Split the nixos-shell module options and config. Allows for the nixos-shell module to be added to system configurations so that flake checks can be build successfully.
1 parent f16f15a commit 21fc93a

File tree

5 files changed

+162
-131
lines changed

5 files changed

+162
-131
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ test-efi:
2424
install:
2525
$(INSTALL) -D bin/nixos-shell $(DESTDIR)$(PREFIX)/bin/nixos-shell
2626
$(INSTALL) -D share/modules/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/modules/nixos-shell.nix
27+
$(INSTALL) -D share/modules/nixos-shell-config.nix $(DESTDIR)$(PREFIX)/share/modules/nixos-shell-config.nix
2728
$(INSTALL) -D share/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/nixos-shell.nix

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

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

2424
supportedSystems = [ "x86_64-linux" ];
2525
in
2626
{
2727
nixosConfigurations = mapAttrs (_name: config: mkSystem inp.nixpkgs config) vms;
28+
29+
nixosModules.nixos-shell = import ./share/modules/nixos-shell.nix;
2830
}
2931

3032
//
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{ lib, options, config, pkgs, ... }:
2+
3+
let
4+
cfg = config.nixos-shell;
5+
6+
mkVMDefault = lib.mkOverride 900;
7+
in {
8+
config =
9+
let
10+
user = builtins.getEnv "USER";
11+
shell' = builtins.baseNameOf (builtins.getEnv "SHELL");
12+
13+
# fish seems to do funky stuff: https://github.com/Mic92/nixos-shell/issues/42
14+
shell = if shell' == "fish" then "bash" else shell';
15+
in
16+
lib.mkMerge [
17+
# Enable the module of the user's shell for some sensible defaults.
18+
(lib.mkIf (options.programs ? ${shell}.enable && shell != "bash") {
19+
programs.${shell}.enable = mkVMDefault true;
20+
})
21+
22+
(lib.mkIf (pkgs ? ${shell}) {
23+
users.extraUsers.root.shell = mkVMDefault pkgs.${shell};
24+
})
25+
26+
(
27+
let
28+
home = builtins.getEnv "HOME";
29+
in
30+
lib.mkIf (home != "" && cfg.mounts.mountHome) {
31+
users.extraUsers.root.home = lib.mkVMOverride home;
32+
}
33+
)
34+
35+
# Allow passwordless ssh login with the user's key if it exists.
36+
(
37+
let
38+
keys = map (key: "${builtins.getEnv "HOME"}/.ssh/${key}")
39+
[ "id_rsa.pub" "id_ecdsa.pub" "id_ed25519.pub" ];
40+
in
41+
{
42+
users.users.root.openssh.authorizedKeys.keyFiles = lib.filter builtins.pathExists keys;
43+
}
44+
)
45+
46+
{
47+
# Allow the user to login as root without password.
48+
users.extraUsers.root.initialHashedPassword = "";
49+
50+
# see https://wiki.qemu.org/Documentation/9psetup#Performance_Considerations
51+
# == 100M
52+
# FIXME? currently 500K seems to be the limit?
53+
virtualisation.msize = mkVMDefault 104857600;
54+
55+
services =
56+
let
57+
service = if lib.versionAtLeast (lib.versions.majorMinor lib.version) "20.09" then "getty" else "mingetty";
58+
in
59+
{
60+
${service}.helpLine = ''
61+
Log in as "root" with an empty password.
62+
If you are connect via serial console:
63+
Type Ctrl-a c to switch to the qemu console
64+
and `quit` to stop the VM.
65+
'';
66+
};
67+
68+
virtualisation = {
69+
graphics = mkVMDefault false;
70+
memorySize = mkVMDefault 700;
71+
72+
qemu.consoles = lib.mkIf (!config.virtualisation.graphics) [ "tty0" "hvc0" ];
73+
74+
qemu.options =
75+
let
76+
nixProfile = "/nix/var/nix/profiles/per-user/${user}/profile/";
77+
in
78+
lib.optionals (!config.virtualisation.graphics) [
79+
"-serial null"
80+
"-device virtio-serial"
81+
"-chardev stdio,mux=on,id=char0,signal=off"
82+
"-mon chardev=char0,mode=readline"
83+
"-device virtconsole,chardev=char0,nr=0"
84+
] ++
85+
lib.optional cfg.mounts.mountHome "-virtfs local,path=/home,security_model=none,mount_tag=home" ++
86+
lib.optional (cfg.mounts.mountNixProfile && builtins.pathExists nixProfile) "-virtfs local,path=${nixProfile},security_model=none,mount_tag=nixprofile" ++
87+
lib.mapAttrsToList (target: mount: "-virtfs local,path=${builtins.toString mount.target},security_model=none,mount_tag=${mount.tag}") cfg.mounts.extraMounts;
88+
};
89+
90+
# build-vm overrides our filesystem settings in nixos-config
91+
boot.initrd.postMountCommands =
92+
(lib.optionalString cfg.mounts.mountHome ''
93+
mkdir -p $targetRoot/home/
94+
mount -t 9p home $targetRoot/home/ -o trans=virtio,version=9p2000.L,cache=${cfg.mounts.cache},msize=${toString config.virtualisation.msize}
95+
'') +
96+
(lib.optionalString (user != "" && cfg.mounts.mountNixProfile) ''
97+
mkdir -p $targetRoot/nix/var/nix/profiles/per-user/${user}/profile/
98+
mount -t 9p nixprofile $targetRoot/nix/var/nix/profiles/per-user/${user}/profile/ -o trans=virtio,version=9p2000.L,cache=${cfg.mounts.cache},msize=${toString config.virtualisation.msize}
99+
'') +
100+
builtins.concatStringsSep " " (lib.mapAttrsToList
101+
(target: mount: ''
102+
mkdir -p $targetRoot/${target}
103+
mount -t 9p ${mount.tag} $targetRoot/${target} -o trans=virtio,version=9p2000.L,cache=${mount.cache},msize=${toString config.virtualisation.msize}
104+
'')
105+
cfg.mounts.extraMounts);
106+
107+
environment = {
108+
systemPackages = with pkgs; [
109+
xterm # for resize command
110+
];
111+
112+
loginShellInit =
113+
let
114+
pwd = builtins.getEnv "PWD";
115+
term = builtins.getEnv "TERM";
116+
path = builtins.getEnv "PATH";
117+
in
118+
''
119+
# fix terminal size
120+
eval "$(resize)"
121+
122+
${lib.optionalString (pwd != "") "cd '${pwd}' 2>/dev/null"}
123+
${lib.optionalString (term != "") "export TERM='${term}'"}
124+
${lib.optionalString (path != "") "export PATH=\"${path}:$PATH\""}
125+
'';
126+
};
127+
128+
networking.firewall.enable = mkVMDefault false;
129+
}
130+
];
131+
}

share/modules/nixos-shell.nix

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
{ lib, options, config, pkgs, modulesPath, ... }:
1+
{ lib, options, pkgs, modulesPath, ... }:
22

3-
let
4-
cfg = config.nixos-shell;
5-
6-
mkVMDefault = lib.mkOverride 900;
7-
in {
3+
{
84
imports = [
95
"${toString modulesPath}/virtualisation/qemu-vm.nix"
106
];
@@ -63,113 +59,4 @@ in {
6359
};
6460
};
6561
};
66-
67-
config = let
68-
user = builtins.getEnv "USER";
69-
shell' = builtins.baseNameOf (builtins.getEnv "SHELL");
70-
71-
# fish seems to do funky stuff: https://github.com/Mic92/nixos-shell/issues/42
72-
shell = if shell' == "fish" then "bash" else shell';
73-
in lib.mkMerge [
74-
# Enable the module of the user's shell for some sensible defaults.
75-
(lib.mkIf (options.programs ? ${shell}.enable && shell != "bash") {
76-
programs.${shell}.enable = mkVMDefault true;
77-
})
78-
79-
(lib.mkIf (pkgs ? ${shell}) {
80-
users.extraUsers.root.shell = mkVMDefault pkgs.${shell};
81-
})
82-
83-
(let
84-
home = builtins.getEnv "HOME";
85-
in lib.mkIf (home != "" && cfg.mounts.mountHome) {
86-
users.extraUsers.root.home = lib.mkVMOverride home;
87-
})
88-
89-
# Allow passwordless ssh login with the user's key if it exists.
90-
(let
91-
keys = map (key: "${builtins.getEnv "HOME"}/.ssh/${key}")
92-
["id_rsa.pub" "id_ecdsa.pub" "id_ed25519.pub"];
93-
in {
94-
users.users.root.openssh.authorizedKeys.keyFiles = lib.filter builtins.pathExists keys;
95-
})
96-
97-
{
98-
# Allow the user to login as root without password.
99-
users.extraUsers.root.initialHashedPassword = "";
100-
101-
# see https://wiki.qemu.org/Documentation/9psetup#Performance_Considerations
102-
# == 100M
103-
# FIXME? currently 500K seems to be the limit?
104-
virtualisation.msize = mkVMDefault 104857600;
105-
106-
services = let
107-
service = if lib.versionAtLeast (lib.versions.majorMinor lib.version) "20.09" then "getty" else "mingetty";
108-
in {
109-
${service}.helpLine = ''
110-
Log in as "root" with an empty password.
111-
If you are connect via serial console:
112-
Type Ctrl-a c to switch to the qemu console
113-
and `quit` to stop the VM.
114-
'';
115-
};
116-
117-
virtualisation = {
118-
graphics = mkVMDefault false;
119-
memorySize = mkVMDefault 700;
120-
121-
qemu.consoles = lib.mkIf (!config.virtualisation.graphics) ["tty0" "hvc0"];
122-
123-
qemu.options = let
124-
nixProfile = "/nix/var/nix/profiles/per-user/${user}/profile/";
125-
in
126-
lib.optionals (!config.virtualisation.graphics) [
127-
"-serial null"
128-
"-device virtio-serial"
129-
"-chardev stdio,mux=on,id=char0,signal=off"
130-
"-mon chardev=char0,mode=readline"
131-
"-device virtconsole,chardev=char0,nr=0"
132-
] ++
133-
lib.optional cfg.mounts.mountHome "-virtfs local,path=/home,security_model=none,mount_tag=home" ++
134-
lib.optional (cfg.mounts.mountNixProfile && builtins.pathExists nixProfile) "-virtfs local,path=${nixProfile},security_model=none,mount_tag=nixprofile" ++
135-
lib.mapAttrsToList (target: mount: "-virtfs local,path=${builtins.toString mount.target},security_model=none,mount_tag=${mount.tag}") cfg.mounts.extraMounts;
136-
};
137-
138-
# build-vm overrides our filesystem settings in nixos-config
139-
boot.initrd.postMountCommands =
140-
(lib.optionalString cfg.mounts.mountHome ''
141-
mkdir -p $targetRoot/home/
142-
mount -t 9p home $targetRoot/home/ -o trans=virtio,version=9p2000.L,cache=${cfg.mounts.cache},msize=${toString config.virtualisation.msize}
143-
'') +
144-
(lib.optionalString (user != "" && cfg.mounts.mountNixProfile) ''
145-
mkdir -p $targetRoot/nix/var/nix/profiles/per-user/${user}/profile/
146-
mount -t 9p nixprofile $targetRoot/nix/var/nix/profiles/per-user/${user}/profile/ -o trans=virtio,version=9p2000.L,cache=${cfg.mounts.cache},msize=${toString config.virtualisation.msize}
147-
'') +
148-
builtins.concatStringsSep " " (lib.mapAttrsToList (target: mount: ''
149-
mkdir -p $targetRoot/${target}
150-
mount -t 9p ${mount.tag} $targetRoot/${target} -o trans=virtio,version=9p2000.L,cache=${mount.cache},msize=${toString config.virtualisation.msize}
151-
'') cfg.mounts.extraMounts);
152-
153-
environment = {
154-
systemPackages = with pkgs; [
155-
xterm # for resize command
156-
];
157-
158-
loginShellInit = let
159-
pwd = builtins.getEnv "PWD";
160-
term = builtins.getEnv "TERM";
161-
path = builtins.getEnv "PATH";
162-
in ''
163-
# fix terminal size
164-
eval "$(resize)"
165-
166-
${lib.optionalString (pwd != "") "cd '${pwd}' 2>/dev/null"}
167-
${lib.optionalString (term != "") "export TERM='${term}'"}
168-
${lib.optionalString (path != "") "export PATH=\"${path}:$PATH\""}
169-
'';
170-
};
171-
172-
networking.firewall.enable = mkVMDefault false;
173-
}
174-
];
17562
}

share/nixos-shell.nix

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
{
2-
nixpkgs ? <nixpkgs>
1+
{ nixpkgs ? <nixpkgs>
32
, system ? builtins.currentSystem
43
, configuration ? <nixos-config>
54

65
, flakeUri ? null
76
, flakeAttr ? null
87
}:
9-
let
10-
nixos-shell-module = import ./modules/nixos-shell.nix;
8+
let
9+
nixos-shell = import ./modules/nixos-shell.nix;
10+
nixos-shell-config = import ./modules/nixos-shell-config.nix;
1111

1212
flake = builtins.getFlake flakeUri;
1313
flakeSystem = flake.outputs.packages."${system}".nixosConfigurations."${flakeAttr}" or flake.outputs.nixosConfigurations."${flakeAttr}";
1414
in
15-
if flakeUri != null then
16-
flakeSystem.override (attrs: {
17-
modules = attrs.modules ++ [ nixos-shell-module ];
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;
1827
})
19-
else
20-
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
21-
inherit system;
22-
modules = [
23-
configuration
24-
nixos-shell-module
25-
];
26-
}
28+
else
29+
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
30+
inherit system;
31+
modules = [
32+
configuration
33+
nixos-shell
34+
nixos-shell-config
35+
];
36+
}

0 commit comments

Comments
 (0)