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