1# This module generates nixos-install, nixos-rebuild,
2# nixos-generate-config, etc.
3
4{
5 config,
6 lib,
7 pkgs,
8 options,
9 ...
10}:
11
12let
13 makeProg =
14 args:
15 pkgs.replaceVarsWith (
16 args
17 // {
18 dir = "bin";
19 isExecutable = true;
20 nativeBuildInputs = [
21 pkgs.installShellFiles
22 ];
23 postInstall = ''
24 installManPage ${args.manPage}
25 '';
26 }
27 );
28
29 nixos-generate-config = makeProg {
30 name = "nixos-generate-config";
31 src = ./nixos-generate-config.pl;
32 replacements = {
33 perl = "${
34 pkgs.perl.withPackages (p: [
35 p.FileSlurp
36 p.ConfigIniFiles
37 ])
38 }/bin/perl";
39 hostPlatformSystem = pkgs.stdenv.hostPlatform.system;
40 detectvirt = "${config.systemd.package}/bin/systemd-detect-virt";
41 btrfs = "${pkgs.btrfs-progs}/bin/btrfs";
42 inherit (config.system.nixos-generate-config) configuration desktopConfiguration flake;
43 xserverEnabled = config.services.xserver.enable;
44 };
45 manPage = ./manpages/nixos-generate-config.8;
46 };
47
48 nixos-version = makeProg {
49 name = "nixos-version";
50 src = ./nixos-version.sh;
51 replacements = {
52 inherit (pkgs) runtimeShell;
53 inherit (config.system.nixos) version codeName revision;
54 inherit (config.system) configurationRevision;
55 json = builtins.toJSON (
56 {
57 nixosVersion = config.system.nixos.version;
58 }
59 // lib.optionalAttrs (config.system.nixos.revision != null) {
60 nixpkgsRevision = config.system.nixos.revision;
61 }
62 // lib.optionalAttrs (config.system.configurationRevision != null) {
63 configurationRevision = config.system.configurationRevision;
64 }
65 );
66 };
67 manPage = ./manpages/nixos-version.8;
68 };
69
70 nixos-install = pkgs.nixos-install.override { };
71 nixos-rebuild = pkgs.nixos-rebuild.override { nix = config.nix.package; };
72 nixos-rebuild-ng = pkgs.nixos-rebuild-ng.override {
73 nix = config.nix.package;
74 withNgSuffix = false;
75 withReexec = true;
76 };
77
78 defaultFlakeTemplate = ''
79 {
80 inputs = {
81 # This is pointing to an unstable release.
82 # If you prefer a stable release instead, you can this to the latest number shown here: https://nixos.org/download
83 # i.e. nixos-24.11
84 # Use `nix flake update` to update the flake to the latest revision of the chosen release channel.
85 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
86 };
87 outputs = inputs\@{ self, nixpkgs, ... }: {
88 # NOTE: '${options.networking.hostName.default}' is the default hostname
89 nixosConfigurations.${options.networking.hostName.default} = nixpkgs.lib.nixosSystem {
90 modules = [ ./configuration.nix ];
91 };
92 };
93 }
94 '';
95
96 defaultConfigTemplate = ''
97 # Edit this configuration file to define what should be installed on
98 # your system. Help is available in the configuration.nix(5) man page, on
99 # https://search.nixos.org/options and in the NixOS manual (`nixos-help`).
100
101 { config, lib, pkgs, ... }:
102
103 {
104 imports =
105 [ # Include the results of the hardware scan.
106 ./hardware-configuration.nix
107 ];
108
109 $bootLoaderConfig
110 # networking.hostName = "nixos"; # Define your hostname.
111
112 # Configure network connections interactively with nmcli or nmtui.
113 networking.networkmanager.enable = true;
114
115 # Set your time zone.
116 # time.timeZone = "Europe/Amsterdam";
117
118 # Configure network proxy if necessary
119 # networking.proxy.default = "http://user:password\@proxy:port/";
120 # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";
121
122 # Select internationalisation properties.
123 # i18n.defaultLocale = "en_US.UTF-8";
124 # console = {
125 # font = "Lat2-Terminus16";
126 # keyMap = "us";
127 # useXkbConfig = true; # use xkb.options in tty.
128 # };
129
130 $xserverConfig
131
132 $desktopConfiguration
133 # Configure keymap in X11
134 # services.xserver.xkb.layout = "us";
135 # services.xserver.xkb.options = "eurosign:e,caps:escape";
136
137 # Enable CUPS to print documents.
138 # services.printing.enable = true;
139
140 # Enable sound.
141 # services.pulseaudio.enable = true;
142 # OR
143 # services.pipewire = {
144 # enable = true;
145 # pulse.enable = true;
146 # };
147
148 # Enable touchpad support (enabled default in most desktopManager).
149 # services.libinput.enable = true;
150
151 # Define a user account. Don't forget to set a password with ‘passwd’.
152 # users.users.alice = {
153 # isNormalUser = true;
154 # extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
155 # packages = with pkgs; [
156 # tree
157 # ];
158 # };
159
160 # programs.firefox.enable = true;
161
162 # List packages installed in system profile.
163 # You can use https://search.nixos.org/ to find more packages (and options).
164 # environment.systemPackages = with pkgs; [
165 # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
166 # wget
167 # ];
168
169 # Some programs need SUID wrappers, can be configured further or are
170 # started in user sessions.
171 # programs.mtr.enable = true;
172 # programs.gnupg.agent = {
173 # enable = true;
174 # enableSSHSupport = true;
175 # };
176
177 # List services that you want to enable:
178
179 # Enable the OpenSSH daemon.
180 # services.openssh.enable = true;
181
182 # Open ports in the firewall.
183 # networking.firewall.allowedTCPPorts = [ ... ];
184 # networking.firewall.allowedUDPPorts = [ ... ];
185 # Or disable the firewall altogether.
186 # networking.firewall.enable = false;
187
188 # Copy the NixOS configuration file and link it from the resulting system
189 # (/run/current-system/configuration.nix). This is useful in case you
190 # accidentally delete configuration.nix.
191 # system.copySystemConfiguration = true;
192
193 # This option defines the first version of NixOS you have installed on this particular machine,
194 # and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
195 #
196 # Most users should NEVER change this value after the initial install, for any reason,
197 # even if you've upgraded your system to a new NixOS release.
198 #
199 # This value does NOT affect the Nixpkgs version your packages and OS are pulled from,
200 # so changing it will NOT upgrade your system - see https://nixos.org/manual/nixos/stable/#sec-upgrading for how
201 # to actually do that.
202 #
203 # This value being lower than the current NixOS release does NOT mean your system is
204 # out of date, out of support, or vulnerable.
205 #
206 # Do NOT change this value unless you have manually inspected all the changes it would make to your configuration,
207 # and migrated your data accordingly.
208 #
209 # For more information, see `man configuration.nix` or https://nixos.org/manual/nixos/stable/options#opt-system.stateVersion .
210 system.stateVersion = "${config.system.nixos.release}"; # Did you read the comment?
211
212 }
213 '';
214in
215{
216 options.system.nixos-generate-config = {
217
218 flake = lib.mkOption {
219 internal = true;
220 type = lib.types.str;
221 default = defaultFlakeTemplate;
222 description = ''
223 The NixOS module that `nixos-generate-config`
224 saves to `/etc/nixos/flake.nix` if --flake is set.
225
226 This is an internal option. No backward compatibility is guaranteed.
227 Use at your own risk!
228
229 Note that this string gets spliced into a Perl script. The perl
230 variable `$bootLoaderConfig` can be used to
231 splice in the boot loader configuration.
232 '';
233 };
234
235 configuration = lib.mkOption {
236 internal = true;
237 type = lib.types.str;
238 default = defaultConfigTemplate;
239 description = ''
240 The NixOS module that `nixos-generate-config`
241 saves to `/etc/nixos/configuration.nix`.
242
243 This is an internal option. No backward compatibility is guaranteed.
244 Use at your own risk!
245
246 Note that this string gets spliced into a Perl script. The perl
247 variable `$bootLoaderConfig` can be used to
248 splice in the boot loader configuration.
249 '';
250 };
251
252 desktopConfiguration = lib.mkOption {
253 internal = true;
254 type = lib.types.listOf lib.types.lines;
255 default = [ ];
256 description = ''
257 Text to preseed the desktop configuration that `nixos-generate-config`
258 saves to `/etc/nixos/configuration.nix`.
259
260 This is an internal option. No backward compatibility is guaranteed.
261 Use at your own risk!
262
263 Note that this string gets spliced into a Perl script. The perl
264 variable `$bootLoaderConfig` can be used to
265 splice in the boot loader configuration.
266 '';
267 };
268 };
269
270 options.system.disableInstallerTools = lib.mkOption {
271 internal = true;
272 type = lib.types.bool;
273 default = false;
274 description = ''
275 Disable nixos-rebuild, nixos-generate-config, nixos-installer
276 and other NixOS tools. This is useful to shrink embedded,
277 read-only systems which are not expected to rebuild or
278 reconfigure themselves. Use at your own risk!
279 '';
280 };
281
282 options.system.rebuild.enableNg = lib.mkEnableOption "" // {
283 default = true;
284 description = ''
285 Whether to use ‘nixos-rebuild-ng’ in place of ‘nixos-rebuild’, the
286 Python-based re-implementation of the original in Bash.
287 '';
288 };
289
290 imports =
291 let
292 mkToolModule =
293 {
294 name,
295 package ? pkgs.${name},
296 }:
297 { config, ... }:
298 {
299 options.system.tools.${name}.enable = lib.mkEnableOption "${name} script" // {
300 default = config.nix.enable && !config.system.disableInstallerTools;
301 defaultText = "config.nix.enable && !config.system.disableInstallerTools";
302 };
303
304 config = lib.mkIf config.system.tools.${name}.enable {
305 environment.systemPackages = [ package ];
306 };
307 };
308 in
309 [
310 (mkToolModule { name = "nixos-build-vms"; })
311 (mkToolModule { name = "nixos-enter"; })
312 (mkToolModule {
313 name = "nixos-generate-config";
314 package = config.system.build.nixos-generate-config;
315 })
316 (mkToolModule {
317 name = "nixos-install";
318 package = config.system.build.nixos-install;
319 })
320 (mkToolModule { name = "nixos-option"; })
321 (mkToolModule {
322 name = "nixos-rebuild";
323 package = config.system.build.nixos-rebuild;
324 })
325 (mkToolModule {
326 name = "nixos-version";
327 package = nixos-version;
328 })
329 ];
330
331 config = {
332 documentation.man.man-db.skipPackages = [ nixos-version ];
333
334 warnings = lib.optional (!config.system.disableInstallerTools && !config.system.rebuild.enableNg) ''
335 The Bash implementation of nixos-rebuild will be deprecated and removed in the 26.05 release of NixOS.
336 Please migrate to the newer implementation by removing 'system.rebuild.enableNg = false' from your configuration.
337 If you are unable to migrate due to any issues with the new implementation, please create an issue and tag the maintainers of 'nixos-rebuild-ng'.
338 '';
339
340 # These may be used in auxiliary scripts (ie not part of toplevel), so they are defined unconditionally.
341 system.build = {
342 inherit nixos-generate-config nixos-install;
343 nixos-rebuild = if config.system.rebuild.enableNg then nixos-rebuild-ng else nixos-rebuild;
344 };
345 };
346}