|
1 | | -{ lib, options, config, pkgs, modulesPath, ... }: |
| 1 | +{ lib, options, pkgs, modulesPath, ... }: |
2 | 2 |
|
3 | | -let |
4 | | - cfg = config.nixos-shell; |
5 | | - |
6 | | - mkVMDefault = lib.mkOverride 900; |
7 | | -in { |
| 3 | +{ |
8 | 4 | imports = [ |
9 | 5 | "${toString modulesPath}/virtualisation/qemu-vm.nix" |
10 | 6 | ]; |
|
63 | 59 | }; |
64 | 60 | }; |
65 | 61 | }; |
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 | | - ]; |
175 | 62 | } |
0 commit comments