@@ -1,8 +1,8 @@
1−{ config, lib, ... }:
1+{ config, lib, pkgs, ... }:
22let
33 inherit (lib) mkOption types optionalString;
44
5− cfg = config.boot.binfmtMiscRegistrations;
5+ cfg = config.boot.binfmt;
66
77 makeBinfmtLine = name: { recognitionType, offset, magicOrExtension
88 , mask, preserveArgvZero, openBinary
@@ -13,125 +13,249 @@ let
1313 mask' = toString mask;
1414 interpreter = "/run/binfmt/${name}";
1515 flags = if !(matchCredentials -> openBinary)
16− then throw "boot.binfmtMiscRegistrations.${name}: you can't specify openBinary = false when matchCredentials = true."
16+ then throw "boot.binfmt.registrations.${name}: you can't specify openBinary = false when matchCredentials = true."
1717 else optionalString preserveArgvZero "P" +
1818 optionalString (openBinary && !matchCredentials) "O" +
1919 optionalString matchCredentials "C" +
2020 optionalString fixBinary "F";
2121 in ":${name}:${type}:${offset'}:${magicOrExtension}:${mask'}:${interpreter}:${flags}";
2222
23− binfmtFile = builtins.toFile "binfmt_nixos.conf"
24− (lib.concatStringsSep "\n" (lib.mapAttrsToList makeBinfmtLine cfg));
25−
2623 activationSnippet = name: { interpreter, ... }:
2724 "ln -sf ${interpreter} /run/binfmt/${name}";
28− activationScript = ''
29− mkdir -p -m 0755 /run/binfmt
30− ${lib.concatStringsSep "\n" (lib.mapAttrsToList activationSnippet cfg)}
31− '';
25+
26+ getEmulator = system: (lib.systems.elaborate { inherit system; }).emulator pkgs;
27+
28+ # Mapping of systems to “magicOrExtension” and “mask”. Mostly taken from:
29+ # - https://github.com/cleverca22/nixos-configs/blob/master/qemu.nix
30+ # and
31+ # - https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
32+ # TODO: maybe put these in a JSON file?
33+ magics = {
34+ armv6l-linux = {
35+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'';
36+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
37+ };
38+ armv7l-linux = {
39+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'';
40+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
41+ };
42+ aarch64-linux = {
43+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'';
44+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
45+ };
46+ aarch64_be-linux = {
47+ magicOrExtension = ''\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7'';
48+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
49+ };
50+ i386-linux = {
51+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00'';
52+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
53+ };
54+ i486-linux = {
55+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'';
56+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
57+ };
58+ i586-linux = {
59+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'';
60+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
61+ };
62+ i686-linux = {
63+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x00'';
64+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
65+ };
66+ x86_64-linux = {
67+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00'';
68+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
69+ };
70+ alpha-linux = {
71+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x26\x90'';
72+ mask = ''\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
73+ };
74+ sparc64-linux = {
75+ magicOrExtension = ''\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02'';
76+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
77+ };
78+ sparc-linux = {
79+ magicOrExtension = ''\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12'';
80+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
81+ };
82+ powerpc-linux = {
83+ magicOrExtension = ''\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14'';
84+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
85+ };
86+ powerpc64-linux = {
87+ magicOrExtension = ''\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15'';
88+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
89+ };
90+ powerpc64le-linux = {
91+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'';
92+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'';
93+ };
94+ mips-linux = {
95+ magicOrExtension = ''\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'';
96+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
97+ };
98+ mipsel-linux = {
99+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'';
100+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
101+ };
102+ mips64-linux = {
103+ magicOrExtension = ''\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08'';
104+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'';
105+ };
106+ mips64el-linux = {
107+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00'';
108+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
109+ };
110+ riscv32-linux = {
111+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'';
112+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
113+ };
114+ riscv64-linux = {
115+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'';
116+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'';
117+ };
118+ x86_64-windows = {
119+ magicOrExtension = ".exe";
120+ recognitionType = "extension";
121+ };
122+ i686-windows = {
123+ magicOrExtension = ".exe";
124+ recognitionType = "extension";
125+ };
126+ };
127+
32128in {
33129 options = {
34− boot.binfmtMiscRegistrations = mkOption {
35− default = {};
36−
37− description = ''
38− Extra binary formats to register with the kernel.
39− See https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html for more details.
40− '';
41−
42− type = types.attrsOf (types.submodule ({ config, ... }: {
43− options = {
44− recognitionType = mkOption {
45− default = "magic";
46− description = "Whether to recognize executables by magic number or extension.";
47− type = types.enum [ "magic" "extension" ];
48− };
130+ boot.binfmt = {
131+ registrations = mkOption {
132+ default = {};
49133
50− offset = mkOption {
51− default = null;
52− description = "The byte offset of the magic number used for recognition.";
53− type = types.nullOr types.int;
54− };
134+ description = ''
135+ Extra binary formats to register with the kernel.
136+ See https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html for more details.
137+ '';
55138
56− magicOrExtension = mkOption {
57− description = "The magic number or extension to match on.";
58− type = types.str;
59− };
139+ type = types.attrsOf (types.submodule ({ config, ... }: {
140+ options = {
141+ recognitionType = mkOption {
142+ default = "magic";
143+ description = "Whether to recognize executables by magic number or extension.";
144+ type = types.enum [ "magic" "extension" ];
145+ };
60146
61− mask = mkOption {
62− default = null;
63− description =
64− "A mask to be ANDed with the byte sequence of the file before matching";
65− type = types.nullOr types.str;
66− };
147+ offset = mkOption {
148+ default = null;
149+ description = "The byte offset of the magic number used for recognition.";
150+ type = types.nullOr types.int;
151+ };
67152
68− interpreter = mkOption {
69− description = ''
70− The interpreter to invoke to run the program.
153+ magicOrExtension = mkOption {
154+ description = "The magic number or extension to match on.";
155+ type = types.str;
156+ };
71157
72− Note that the actual registration will point to
73− /run/binfmt/''${name}, so the kernel interpreter length
74− limit doesn't apply.
75− '';
76− type = types.path;
77− };
158+ mask = mkOption {
159+ default = null;
160+ description =
161+ "A mask to be ANDed with the byte sequence of the file before matching";
162+ type = types.nullOr types.str;
163+ };
78164
79− preserveArgvZero = mkOption {
80− default = false;
81− description = ''
82− Whether to pass the original argv[0] to the interpreter.
165+ interpreter = mkOption {
166+ description = ''
167+ The interpreter to invoke to run the program.
83168
84− See the description of the 'P' flag in the kernel docs
85− for more details;
86− '';
87− type = types.bool;
88− };
169+ Note that the actual registration will point to
170+ /run/binfmt/''${name}, so the kernel interpreter length
171+ limit doesn't apply.
172+ '';
173+ type = types.path;
174+ };
89175
90− openBinary = mkOption {
91− default = config.matchCredentials;
92− description = ''
93− Whether to pass the binary to the interpreter as an open
94− file descriptor, instead of a path.
95− '';
96− type = types.bool;
97− };
176+ preserveArgvZero = mkOption {
177+ default = false;
178+ description = ''
179+ Whether to pass the original argv[0] to the interpreter.
98180
99− matchCredentials = mkOption {
100− default = false;
101− description = ''
102− Whether to launch with the credentials and security
103− token of the binary, not the interpreter (e.g. setuid
104− bit).
181+ See the description of the 'P' flag in the kernel docs
182+ for more details;
183+ '';
184+ type = types.bool;
185+ };
105186
106− See the description of the 'C' flag in the kernel docs
107− for more details.
187+ openBinary = mkOption {
188+ default = config.matchCredentials;
189+ description = ''
190+ Whether to pass the binary to the interpreter as an open
191+ file descriptor, instead of a path.
192+ '';
193+ type = types.bool;
194+ };
108195
109− Implies/requires openBinary = true.
110− '';
111− type = types.bool;
112− };
196+ matchCredentials = mkOption {
197+ default = false;
198+ description = ''
199+ Whether to launch with the credentials and security
200+ token of the binary, not the interpreter (e.g. setuid
201+ bit).
113202
114− fixBinary = mkOption {
115− default = false;
116− description = ''
117− Whether to open the interpreter file as soon as the
118− registration is loaded, rather than waiting for a
119− relevant file to be invoked.
120−
121− See the description of the 'F' flag in the kernel docs
122− for more details.
123− '';
124− type = types.bool;
203+ See the description of the 'C' flag in the kernel docs
204+ for more details.
205+
206+ Implies/requires openBinary = true.
207+ '';
208+ type = types.bool;
209+ };
210+
211+ fixBinary = mkOption {
212+ default = false;
213+ description = ''
214+ Whether to open the interpreter file as soon as the
215+ registration is loaded, rather than waiting for a
216+ relevant file to be invoked.
217+
218+ See the description of the 'F' flag in the kernel docs
219+ for more details.
220+ '';
221+ type = types.bool;
222+ };
125223 };
126− };
127− }));
224+ }));
225+ };
226+
227+ emulatedSystems = mkOption {
228+ default = [];
229+ description = ''
230+ List of systems to emulate. Will also configure Nix to
231+ support your new systems.
232+ '';
233+ type = types.listOf types.string;
234+ };
128235 };
129236 };
130237
131− config = lib.mkIf (cfg != {}) {
132− environment.etc."binfmt.d/nixos.conf".source = binfmtFile;
133− system.activationScripts.binfmt = activationScript;
134− systemd.additionalUpstreamSystemUnits =
238+ config = {
239+ boot.binfmt.registrations = builtins.listToAttrs (map (system: {
240+ name = system;
241+ value = {
242+ interpreter = getEmulator system;
243+ } // (magics.${system} or (throw "Cannot create binfmt registration for system ${system}"));
244+ }) cfg.emulatedSystems);
245+ # TODO: add a nix.extraPlatforms option to NixOS!
246+ nix.extraOptions = lib.mkIf (cfg.emulatedSystems != []) ''
247+ extra-platforms = ${toString (cfg.emulatedSystems ++ lib.optional pkgs.stdenv.hostPlatform.isx86_64 "i686-linux")}
248+ '';
249+ nix.sandboxPaths = lib.mkIf (cfg.emulatedSystems != [])
250+ ([ "/run/binfmt" ] ++ (map (system: dirOf (dirOf (getEmulator system))) cfg.emulatedSystems));
251+
252+ environment.etc."binfmt.d/nixos.conf".source = builtins.toFile "binfmt_nixos.conf"
253+ (lib.concatStringsSep "\n" (lib.mapAttrsToList makeBinfmtLine config.boot.binfmt.registrations));
254+ system.activationScripts.binfmt = ''
255+ mkdir -p -m 0755 /run/binfmt
256+ ${lib.concatStringsSep "\n" (lib.mapAttrsToList activationSnippet config.boot.binfmt.registrations)}
257+ '';
258+ systemd.additionalUpstreamSystemUnits = lib.mkIf (config.boot.binfmt.registrations != {})
135259 [ "proc-sys-fs-binfmt_misc.automount"
136260 "proc-sys-fs-binfmt_misc.mount"
137261 ];