@@ -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,169 @@ 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− '';
32−in {
33− 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− };
49−
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− };
55−
56− magicOrExtension = mkOption {
57− description = "The magic number or extension to match on.";
58− type = types.str;
59− };
6025
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− };
67−
68− interpreter = mkOption {
69− description = ''
70− The interpreter to invoke to run the program.
71−
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− };
78−
79− preserveArgvZero = mkOption {
80− default = false;
81− description = ''
82− Whether to pass the original argv[0] to the interpreter.
83−
84− See the description of the 'P' flag in the kernel docs
85− for more details;
86− '';
87− type = types.bool;
88− };
26+ getEmulator = system: (lib.systems.elaborate { inherit system; }).emulator pkgs;
8927
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− };
98−
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).
105−
106− See the description of the 'C' flag in the kernel docs
107− for more details.
108−
109− Implies/requires openBinary = true.
110− '';
111− type = types.bool;
112− };
113−
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;
28+in {
29+ options = {
30+ boot.binfmt = {
31+ registrations = mkOption {
32+ default = {};
33+
34+ description = ''
35+ Extra binary formats to register with the kernel.
36+ See https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html for more details.
37+ '';
38+
39+ type = types.attrsOf (types.submodule ({ config, ... }: {
40+ options = {
41+ recognitionType = mkOption {
42+ default = "magic";
43+ description = "Whether to recognize executables by magic number or extension.";
44+ type = types.enum [ "magic" "extension" ];
45+ };
46+
47+ offset = mkOption {
48+ default = null;
49+ description = "The byte offset of the magic number used for recognition.";
50+ type = types.nullOr types.int;
51+ };
52+
53+ magicOrExtension = mkOption {
54+ description = "The magic number or extension to match on.";
55+ type = types.str;
56+ };
57+
58+ mask = mkOption {
59+ default = null;
60+ description =
61+ "A mask to be ANDed with the byte sequence of the file before matching";
62+ type = types.nullOr types.str;
63+ };
64+
65+ interpreter = mkOption {
66+ description = ''
67+ The interpreter to invoke to run the program.
68+
69+ Note that the actual registration will point to
70+ /run/binfmt/''${name}, so the kernel interpreter length
71+ limit doesn't apply.
72+ '';
73+ type = types.path;
74+ };
75+
76+ preserveArgvZero = mkOption {
77+ default = false;
78+ description = ''
79+ Whether to pass the original argv[0] to the interpreter.
80+
81+ See the description of the 'P' flag in the kernel docs
82+ for more details;
83+ '';
84+ type = types.bool;
85+ };
86+
87+ openBinary = mkOption {
88+ default = config.matchCredentials;
89+ description = ''
90+ Whether to pass the binary to the interpreter as an open
91+ file descriptor, instead of a path.
92+ '';
93+ type = types.bool;
94+ };
95+
96+ matchCredentials = mkOption {
97+ default = false;
98+ description = ''
99+ Whether to launch with the credentials and security
100+ token of the binary, not the interpreter (e.g. setuid
101+ bit).
102+
103+ See the description of the 'C' flag in the kernel docs
104+ for more details.
105+
106+ Implies/requires openBinary = true.
107+ '';
108+ type = types.bool;
109+ };
110+
111+ fixBinary = mkOption {
112+ default = false;
113+ description = ''
114+ Whether to open the interpreter file as soon as the
115+ registration is loaded, rather than waiting for a
116+ relevant file to be invoked.
117+
118+ See the description of the 'F' flag in the kernel docs
119+ for more details.
120+ '';
121+ type = types.bool;
122+ };
125123 };
126− };
127− }));
124+ }));
125+ };
126+
127+ emulatedSystems = mkOption {
128+ default = [];
129+ description = ''
130+ List of systems to emulate. Will also configure Nix to
131+ support your new systems.
132+ '';
133+ type = types.listOf types.string;
134+ };
128135 };
129136 };
130137
131− config = lib.mkIf (cfg != {}) {
132− environment.etc."binfmt.d/nixos.conf".source = binfmtFile;
133− system.activationScripts.binfmt = activationScript;
134− systemd.additionalUpstreamSystemUnits =
138+ config = {
139+ boot.binfmt.registrations = builtins.listToAttrs (map (system: {
140+ name = system;
141+ value = {
142+ interpreter = getEmulator system;
143+ } // ({
144+ armv7l-linux = {
145+ magicOrExtension = ''\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'';
146+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
147+ };
148+ aarch64-linux = {
149+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00'';
150+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
151+ };
152+ riscv64-linux = {
153+ magicOrExtension = ''\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00'';
154+ mask = ''\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\x00\xff\xfe\xff\xff\xff'';
155+ };
156+ x86_64-windows = {
157+ magicOrExtension = ".exe";
158+ recognitionType = "extension";
159+ };
160+ i686-windows = {
161+ magicOrExtension = ".exe";
162+ recognitionType = "extension";
163+ };
164+ }.${system} or (throw "Cannot create binfmt registration for system ${system}"));
165+ }) cfg.emulatedSystems);
166+ # TODO: add a nix.extraPlatforms option to NixOS!
167+ nix.extraOptions = lib.mkIf (cfg.emulatedSystems != []) ''
168+ extra-platforms = ${toString (cfg.emulatedSystems ++ lib.optional pkgs.stdenv.hostPlatform.isx86_64 "i686-linux")}
169+ '';
170+ # nix.sandboxPaths = lib.mkIf (cfg.emulatedSystems != []) ([ "/run/binfmt" ] ++ (map getEmulator cfg.emulatedSystems));
171+
172+ environment.etc."binfmt.d/nixos.conf".source = builtins.toFile "binfmt_nixos.conf"
173+ (lib.concatStringsSep "\n" (lib.mapAttrsToList makeBinfmtLine cfg.registrations));
174+ system.activationScripts.binfmt = lib.mkIf (cfg.registrations != {}) ''
175+ mkdir -p -m 0755 /run/binfmt
176+ ${lib.concatStringsSep "\n" (lib.mapAttrsToList activationSnippet cfg.registrations)}
177+ '';
178+ systemd.additionalUpstreamSystemUnits = lib.mkIf (cfg.registrations != {})
135179 [ "proc-sys-fs-binfmt_misc.automount"
136180 "proc-sys-fs-binfmt_misc.mount"
137181 ];