1{ config, lib, options, pkgs, ... }:
2
3let
4 cfg = config.system.nixos;
5 opt = options.system.nixos;
6
7 inherit (lib)
8 concatStringsSep mapAttrsToList toLower optionalString
9 literalExpression mkRenamedOptionModule mkDefault mkOption trivial types;
10
11 needsEscaping = s: null != builtins.match "[a-zA-Z0-9]+" s;
12 escapeIfNecessary = s: if needsEscaping s then s else ''"${lib.escape [ "\$" "\"" "\\" "\`" ] s}"'';
13 attrsToText = attrs:
14 concatStringsSep "\n"
15 (mapAttrsToList (n: v: ''${n}=${escapeIfNecessary (toString v)}'') attrs)
16 + "\n";
17
18 osReleaseContents =
19 let
20 isNixos = cfg.distroId == "nixos";
21 in
22 {
23 NAME = "${cfg.distroName}";
24 ID = "${cfg.distroId}";
25 VERSION = "${cfg.release} (${cfg.codeName})";
26 VERSION_CODENAME = toLower cfg.codeName;
27 VERSION_ID = cfg.release;
28 BUILD_ID = cfg.version;
29 PRETTY_NAME = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
30 LOGO = "nix-snowflake";
31 HOME_URL = optionalString isNixos "https://nixos.org/";
32 DOCUMENTATION_URL = optionalString isNixos "https://nixos.org/learn.html";
33 SUPPORT_URL = optionalString isNixos "https://nixos.org/community.html";
34 BUG_REPORT_URL = optionalString isNixos "https://github.com/NixOS/nixpkgs/issues";
35 ANSI_COLOR = optionalString isNixos "1;34";
36 IMAGE_ID = optionalString (config.system.image.id != null) config.system.image.id;
37 IMAGE_VERSION = optionalString (config.system.image.version != null) config.system.image.version;
38 } // lib.optionalAttrs (cfg.variant_id != null) {
39 VARIANT_ID = cfg.variant_id;
40 };
41
42 initrdReleaseContents = (removeAttrs osReleaseContents [ "BUILD_ID" ]) // {
43 PRETTY_NAME = "${osReleaseContents.PRETTY_NAME} (Initrd)";
44 };
45 initrdRelease = pkgs.writeText "initrd-release" (attrsToText initrdReleaseContents);
46
47in
48{
49 imports = [
50 ./label.nix
51 (mkRenamedOptionModule [ "system" "nixosVersion" ] [ "system" "nixos" "version" ])
52 (mkRenamedOptionModule [ "system" "nixosVersionSuffix" ] [ "system" "nixos" "versionSuffix" ])
53 (mkRenamedOptionModule [ "system" "nixosRevision" ] [ "system" "nixos" "revision" ])
54 (mkRenamedOptionModule [ "system" "nixosLabel" ] [ "system" "nixos" "label" ])
55 ];
56
57 options.boot.initrd.osRelease = mkOption {
58 internal = true;
59 readOnly = true;
60 default = initrdRelease;
61 };
62
63 options.system = {
64 nixos = {
65 version = mkOption {
66 internal = true;
67 type = types.str;
68 description = "The full NixOS version (e.g. `16.03.1160.f2d4ee1`).";
69 };
70
71 release = mkOption {
72 readOnly = true;
73 type = types.str;
74 default = trivial.release;
75 description = "The NixOS release (e.g. `16.03`).";
76 };
77
78 versionSuffix = mkOption {
79 internal = true;
80 type = types.str;
81 default = trivial.versionSuffix;
82 description = "The NixOS version suffix (e.g. `1160.f2d4ee1`).";
83 };
84
85 revision = mkOption {
86 internal = true;
87 type = types.nullOr types.str;
88 default = trivial.revisionWithDefault null;
89 description = "The Git revision from which this NixOS configuration was built.";
90 };
91
92 codeName = mkOption {
93 readOnly = true;
94 type = types.str;
95 default = trivial.codeName;
96 description = "The NixOS release code name (e.g. `Emu`).";
97 };
98
99 distroId = mkOption {
100 internal = true;
101 type = types.str;
102 default = "nixos";
103 description = "The id of the operating system";
104 };
105
106 distroName = mkOption {
107 internal = true;
108 type = types.str;
109 default = "NixOS";
110 description = "The name of the operating system";
111 };
112
113 variant_id = mkOption {
114 type = types.nullOr (types.strMatching "^[a-z0-9._-]+$");
115 default = null;
116 description = "A lower-case string identifying a specific variant or edition of the operating system";
117 example = "installer";
118 };
119 };
120
121 image = {
122
123 id = lib.mkOption {
124 type = types.nullOr (types.strMatching "^[a-z0-9._-]+$");
125 default = null;
126 description = ''
127 Image identifier.
128
129 This corresponds to the IMAGE_ID field in os-release. See the
130 upstream docs for more details on valid characters for this field:
131 https://www.freedesktop.org/software/systemd/man/latest/os-release.html#IMAGE_ID=
132
133 You would only want to set this option if you're build NixOS appliance images.
134 '';
135 };
136
137 version = lib.mkOption {
138 type = types.nullOr (types.strMatching "^[a-z0-9._-~^]+$");
139 default = null;
140 description = ''
141 Image version.
142
143 This corresponds to the IMAGE_VERSION field in os-release. See the
144 upstream docs for more details on valid characters for this field:
145 https://www.freedesktop.org/software/systemd/man/latest/os-release.html#IMAGE_VERSION=
146
147 You would only want to set this option if you're build NixOS appliance images.
148 '';
149 };
150
151 };
152
153 stateVersion = mkOption {
154 type = types.str;
155 # TODO Remove this and drop the default of the option so people are forced to set it.
156 # Doing this also means fixing the comment in nixos/modules/testing/test-instrumentation.nix
157 apply = v:
158 lib.warnIf (options.system.stateVersion.highestPrio == (lib.mkOptionDefault { }).priority)
159 "system.stateVersion is not set, defaulting to ${v}. Read why this matters on https://nixos.org/manual/nixos/stable/options.html#opt-system.stateVersion."
160 v;
161 default = cfg.release;
162 defaultText = literalExpression "config.${opt.release}";
163 description = ''
164 This option defines the first version of NixOS you have installed on this particular machine,
165 and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
166
167 For example, if NixOS version XX.YY ships with AwesomeDB version N by default, and is then
168 upgraded to version XX.YY+1, which ships AwesomeDB version N+1, the existing databases
169 may no longer be compatible, causing applications to fail, or even leading to data loss.
170
171 The `stateVersion` mechanism avoids this situation by making the default version of such packages
172 conditional on the first version of NixOS you've installed (encoded in `stateVersion`), instead of
173 simply always using the latest one.
174
175 Note that this generally only affects applications that can't upgrade their data automatically -
176 applications and services supporting automatic migrations will remain on latest versions when
177 you upgrade.
178
179 Most users should **never** change this value after the initial install, for any reason,
180 even if you've upgraded your system to a new NixOS release.
181
182 This value does **not** affect the Nixpkgs version your packages and OS are pulled from,
183 so changing it will **not** upgrade your system.
184
185 This value being lower than the current NixOS release does **not** mean your system is
186 out of date, out of support, or vulnerable.
187
188 Do **not** change this value unless you have manually inspected all the changes it would
189 make to your configuration, and migrated your data accordingly.
190 '';
191 };
192
193 configurationRevision = mkOption {
194 type = types.nullOr types.str;
195 default = null;
196 description = "The Git revision of the top-level flake from which this configuration was built.";
197 };
198
199 };
200
201 config = {
202
203 system.nixos = {
204 # These defaults are set here rather than up there so that
205 # changing them would not rebuild the manual
206 version = mkDefault (cfg.release + cfg.versionSuffix);
207 };
208
209 # Generate /etc/os-release. See
210 # https://www.freedesktop.org/software/systemd/man/os-release.html for the
211 # format.
212 environment.etc = {
213 "lsb-release".text = attrsToText {
214 LSB_VERSION = "${cfg.release} (${cfg.codeName})";
215 DISTRIB_ID = "${cfg.distroId}";
216 DISTRIB_RELEASE = cfg.release;
217 DISTRIB_CODENAME = toLower cfg.codeName;
218 DISTRIB_DESCRIPTION = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
219 };
220
221 "os-release".text = attrsToText osReleaseContents;
222 };
223
224 };
225
226 # uses version info nixpkgs, which requires a full nixpkgs path
227 meta.buildDocsInSandbox = false;
228}