-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpackage.nix
212 lines (184 loc) · 7.24 KB
/
package.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{
# dependencies
nixos-generators,
nixpkgs,
# pkgs
lib,
mount,
umount,
# configuration
linuxSystem,
debugInsecurely ? false, # enable auto-login and passwordless sudo to root
extraConfig ? {},
onDemand ? false, # enable launchd socket activation
withRosetta ? true,
}:
nixos-generators.nixosGenerate (
let
inherit
(lib)
escapeShellArg
optionalAttrs
optionals
;
inherit
(import ./constants.nix)
linuxHostName
linuxUser
sshHostPrivateKeyFileName
sshUserPublicKeyFileName
;
imageFormat = "qcow-efi"; # must match `vmYaml.images.location`s extension
sshdKeys = "sshd-keys";
sshDirPath = "/etc/ssh";
sshHostPrivateKeyFilePath = "${sshDirPath}/${sshHostPrivateKeyFileName}";
in {
format = imageFormat;
modules =
[
{
boot = {
kernelParams = ["console=tty0"];
loader = {
efi.canTouchEfiVariables = true;
systemd-boot.enable = true;
};
};
documentation.enable = false;
fileSystems = {
"/".options = [
"discard"
"noatime"
];
"/boot".options = [
"discard"
"noatime"
"umask=0077"
];
};
networking.hostName = linuxHostName;
nix = {
channel.enable = false;
registry.nixpkgs.flake = nixpkgs;
settings = {
auto-optimise-store = true;
experimental-features = [
"flakes"
"nix-command"
];
min-free = "5G";
max-free = "7G";
trusted-users = [linuxUser];
};
};
security = {
sudo = {
enable = debugInsecurely;
wheelNeedsPassword = !debugInsecurely;
};
};
services = {
getty = optionalAttrs debugInsecurely {autologinUser = linuxUser;};
logind = optionalAttrs onDemand {
extraConfig = ''
IdleAction=poweroff
IdleActionSec=3h
'';
};
openssh = {
enable = true;
hostKeys = []; # disable automatic host key generation
settings = {
HostKey = sshHostPrivateKeyFilePath;
PasswordAuthentication = false;
};
};
};
system = {
disableInstallerTools = true;
stateVersion = "24.05";
};
# macOS' Virtualization framework's virtiofs implementation will grant any guest user access
# to mounted files; they always appear to be owned by the effective UID and so access cannot
# be restricted.
# To protect the guest's SSH host key, the VM is configured to prevent any logins (via
# console, SSH, etc) by default. This service then runs before sshd, mounts virtiofs,
# copies the keys to local files (with appropriate ownership and permissions), and unmounts
# the filesystem before allowing SSH to start.
# Once SSH has been allowed to start (and given the guest user a chance to log in), the
# virtiofs must never be mounted again (as the user could have left some process active to
# read its secrets). This is prevented by `unitconfig.ConditionPathExists` below.
systemd.services."${sshdKeys}" = let
# Lima labels its virtiofs folder mounts counting up:
# https://github.com/lima-vm/lima/blob/0e931107cadbcb6dbc7bbb25626f66cdbca1f040/pkg/vz/vm_darwin.go#L568
# So this suffix must match `vmYaml.mounts.location`s order:
sshdKeysVirtiofsTag = "mount0";
sshdKeysDirPath = "/var/${sshdKeys}";
sshAuthorizedKeysUserFilePath = "${sshDirPath}/authorized_keys.d/${linuxUser}";
sshdService = "sshd.service";
in {
before = [sshdService];
description = "Install sshd's host and authorized keys";
enableStrictShellChecks = true;
path = [
mount
umount
];
requiredBy = [sshdService];
script = let
sshAuthorizedKeysUserFilePathSh = escapeShellArg sshAuthorizedKeysUserFilePath;
sshAuthorizedKeysUserTmpFilePathSh = escapeShellArg "${sshAuthorizedKeysUserFilePath}.tmp";
sshHostPrivateKeyFileNameSh = escapeShellArg sshHostPrivateKeyFileName;
sshHostPrivateKeyFilePathSh = escapeShellArg sshHostPrivateKeyFilePath;
sshUserPublicKeyFileNameSh = escapeShellArg sshUserPublicKeyFileName;
sshdKeysDirPathSh = escapeShellArg sshdKeysDirPath;
sshdKeysVirtiofsTagSh = escapeShellArg sshdKeysVirtiofsTag;
in ''
# must be idempotent in the face of partial failues
mkdir -p ${sshdKeysDirPathSh}
mount \
-t 'virtiofs' \
-o 'nodev,noexec,nosuid,ro' \
${sshdKeysVirtiofsTagSh} \
${sshdKeysDirPathSh}
mkdir -p "$(dirname ${sshHostPrivateKeyFilePathSh})"
(
umask 'go='
cp ${sshdKeysDirPathSh}/${sshHostPrivateKeyFileNameSh} ${sshHostPrivateKeyFilePathSh}
)
mkdir -p "$(dirname ${sshAuthorizedKeysUserTmpFilePathSh})"
cp \
${sshdKeysDirPathSh}/${sshUserPublicKeyFileNameSh} \
${sshAuthorizedKeysUserTmpFilePathSh}
chmod 'a+r' ${sshAuthorizedKeysUserTmpFilePathSh}
umount ${sshdKeysDirPathSh}
rmdir ${sshdKeysDirPathSh}
# must be last so only now `unitConfig.ConditionPathExists` triggers
mv ${sshAuthorizedKeysUserTmpFilePathSh} ${sshAuthorizedKeysUserFilePathSh}
'';
serviceConfig.Type = "oneshot";
# see comments on this service and in its `script`
unitConfig.ConditionPathExists = "!${sshAuthorizedKeysUserFilePath}";
};
users = {
# console and (initial) SSH logins are purposely disabled
# see: `systemd.services."${sshdKeys}"`
allowNoPasswordLogin = true;
mutableUsers = false;
users."${linuxUser}" = {
isNormalUser = true;
extraGroups = optionals debugInsecurely ["wheel"];
};
};
virtualisation.rosetta = optionalAttrs withRosetta {
enable = true;
# Lima's virtiofs label for rosetta:
# https://github.com/lima-vm/lima/blob/0e931107cadbcb6dbc7bbb25626f66cdbca1f040/pkg/vz/rosetta_directory_share_arm64.go#L15
mountTag = "vz-rosetta";
};
}
]
++ [extraConfig];
system = linuxSystem;
}
)