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