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 });
13
14 nixos-build-vms = makeProg {
15 name = "nixos-build-vms";
16 src = ./nixos-build-vms/nixos-build-vms.sh;
17 inherit (pkgs) runtimeShell;
18 };
19
20 nixos-install = makeProg {
21 name = "nixos-install";
22 src = ./nixos-install.sh;
23 inherit (pkgs) runtimeShell;
24 nix = config.nix.package.out;
25 path = makeBinPath [
26 pkgs.jq
27 nixos-enter
28 pkgs.util-linuxMinimal
29 ];
30 };
31
32 nixos-rebuild = pkgs.nixos-rebuild.override { nix = config.nix.package.out; };
33
34 nixos-generate-config = makeProg {
35 name = "nixos-generate-config";
36 src = ./nixos-generate-config.pl;
37 perl = "${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl";
38 system = pkgs.stdenv.hostPlatform.system;
39 detectvirt = "${config.systemd.package}/bin/systemd-detect-virt";
40 btrfs = "${pkgs.btrfs-progs}/bin/btrfs";
41 inherit (config.system.nixos-generate-config) configuration desktopConfiguration;
42 xserverEnabled = config.services.xserver.enable;
43 };
44
45 nixos-option =
46 if lib.versionAtLeast (lib.getVersion config.nix.package) "2.4pre"
47 then null
48 else pkgs.nixos-option;
49
50 nixos-version = makeProg {
51 name = "nixos-version";
52 src = ./nixos-version.sh;
53 inherit (pkgs) runtimeShell;
54 inherit (config.system.nixos) version codeName revision;
55 inherit (config.system) configurationRevision;
56 json = builtins.toJSON ({
57 nixosVersion = config.system.nixos.version;
58 } // optionalAttrs (config.system.nixos.revision != null) {
59 nixpkgsRevision = config.system.nixos.revision;
60 } // optionalAttrs (config.system.configurationRevision != null) {
61 configurationRevision = config.system.configurationRevision;
62 });
63 };
64
65 nixos-enter = makeProg {
66 name = "nixos-enter";
67 src = ./nixos-enter.sh;
68 inherit (pkgs) runtimeShell;
69 path = makeBinPath [
70 pkgs.util-linuxMinimal
71 ];
72 };
73
74in
75
76{
77
78 options.system.nixos-generate-config = {
79 configuration = mkOption {
80 internal = true;
81 type = types.str;
82 description = lib.mdDoc ''
83 The NixOS module that `nixos-generate-config`
84 saves to `/etc/nixos/configuration.nix`.
85
86 This is an internal option. No backward compatibility is guaranteed.
87 Use at your own risk!
88
89 Note that this string gets spliced into a Perl script. The perl
90 variable `$bootLoaderConfig` can be used to
91 splice in the boot loader configuration.
92 '';
93 };
94
95 desktopConfiguration = mkOption {
96 internal = true;
97 type = types.listOf types.lines;
98 default = [];
99 description = lib.mdDoc ''
100 Text to preseed the desktop configuration that `nixos-generate-config`
101 saves to `/etc/nixos/configuration.nix`.
102
103 This is an internal option. No backward compatibility is guaranteed.
104 Use at your own risk!
105
106 Note that this string gets spliced into a Perl script. The perl
107 variable `$bootLoaderConfig` can be used to
108 splice in the boot loader configuration.
109 '';
110 };
111 };
112
113 options.system.disableInstallerTools = mkOption {
114 internal = true;
115 type = types.bool;
116 default = false;
117 description = lib.mdDoc ''
118 Disable nixos-rebuild, nixos-generate-config, nixos-installer
119 and other NixOS tools. This is useful to shrink embedded,
120 read-only systems which are not expected to be rebuild or
121 reconfigure themselves. Use at your own risk!
122 '';
123 };
124
125 config = lib.mkIf (config.nix.enable && !config.system.disableInstallerTools) {
126
127 system.nixos-generate-config.configuration = mkDefault ''
128 # Edit this configuration file to define what should be installed on
129 # your system. Help is available in the configuration.nix(5) man page
130 # and in the NixOS manual (accessible by running `nixos-help`).
131
132 { config, pkgs, ... }:
133
134 {
135 imports =
136 [ # Include the results of the hardware scan.
137 ./hardware-configuration.nix
138 ];
139
140 $bootLoaderConfig
141 # networking.hostName = "nixos"; # Define your hostname.
142 # Pick only one of the below networking options.
143 # networking.wireless.enable = true; # Enables wireless support via wpa_supplicant.
144 # networking.networkmanager.enable = true; # Easiest to use and most distros use this by default.
145
146 # Set your time zone.
147 # time.timeZone = "Europe/Amsterdam";
148
149 # Configure network proxy if necessary
150 # networking.proxy.default = "http://user:password\@proxy:port/";
151 # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";
152
153 # Select internationalisation properties.
154 # i18n.defaultLocale = "en_US.UTF-8";
155 # console = {
156 # font = "Lat2-Terminus16";
157 # keyMap = "us";
158 # useXkbConfig = true; # use xkbOptions in tty.
159 # };
160
161 $xserverConfig
162
163 $desktopConfiguration
164 # Configure keymap in X11
165 # services.xserver.layout = "us";
166 # services.xserver.xkbOptions = "eurosign:e,caps:escape";
167
168 # Enable CUPS to print documents.
169 # services.printing.enable = true;
170
171 # Enable sound.
172 # sound.enable = true;
173 # hardware.pulseaudio.enable = true;
174
175 # Enable touchpad support (enabled default in most desktopManager).
176 # services.xserver.libinput.enable = true;
177
178 # Define a user account. Don't forget to set a password with ‘passwd’.
179 # users.users.alice = {
180 # isNormalUser = true;
181 # extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
182 # packages = with pkgs; [
183 # firefox
184 # tree
185 # ];
186 # };
187
188 # List packages installed in system profile. To search, run:
189 # \$ nix search wget
190 # environment.systemPackages = with pkgs; [
191 # vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
192 # wget
193 # ];
194
195 # Some programs need SUID wrappers, can be configured further or are
196 # started in user sessions.
197 # programs.mtr.enable = true;
198 # programs.gnupg.agent = {
199 # enable = true;
200 # enableSSHSupport = true;
201 # };
202
203 # List services that you want to enable:
204
205 # Enable the OpenSSH daemon.
206 # services.openssh.enable = true;
207
208 # Open ports in the firewall.
209 # networking.firewall.allowedTCPPorts = [ ... ];
210 # networking.firewall.allowedUDPPorts = [ ... ];
211 # Or disable the firewall altogether.
212 # networking.firewall.enable = false;
213
214 # Copy the NixOS configuration file and link it from the resulting system
215 # (/run/current-system/configuration.nix). This is useful in case you
216 # accidentally delete configuration.nix.
217 # system.copySystemConfiguration = true;
218
219 # This value determines the NixOS release from which the default
220 # settings for stateful data, like file locations and database versions
221 # on your system were taken. It's perfectly fine and recommended to leave
222 # this value at the release version of the first install of this system.
223 # Before changing this value read the documentation for this option
224 # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
225 system.stateVersion = "${config.system.nixos.release}"; # Did you read the comment?
226
227 }
228 '';
229
230 environment.systemPackages =
231 [ nixos-build-vms
232 nixos-install
233 nixos-rebuild
234 nixos-generate-config
235 nixos-version
236 nixos-enter
237 ] ++ lib.optional (nixos-option != null) nixos-option;
238
239 documentation.man.man-db.skipPackages = [ nixos-version ];
240
241 system.build = {
242 inherit nixos-install nixos-generate-config nixos-option nixos-rebuild nixos-enter;
243 };
244
245 };
246
247}