Release NixOS 23.11
[NixPkgs.git] / nixos / modules / misc / version.nix
blob2837004a8df2d62efc8c1de4547e7db0d7be3f4b
1 { config, lib, options, pkgs, ... }:
3 let
4   cfg = config.system.nixos;
5   opt = options.system.nixos;
7   inherit (lib)
8     concatStringsSep mapAttrsToList toLower
9     literalExpression mkRenamedOptionModule mkDefault mkOption trivial types;
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";
18   osReleaseContents = {
19     NAME = "${cfg.distroName}";
20     ID = "${cfg.distroId}";
21     VERSION = "${cfg.release} (${cfg.codeName})";
22     VERSION_CODENAME = toLower cfg.codeName;
23     VERSION_ID = cfg.release;
24     BUILD_ID = cfg.version;
25     PRETTY_NAME = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
26     LOGO = "nix-snowflake";
27     HOME_URL = lib.optionalString (cfg.distroId == "nixos") "https://nixos.org/";
28     DOCUMENTATION_URL = lib.optionalString (cfg.distroId == "nixos") "https://nixos.org/learn.html";
29     SUPPORT_URL = lib.optionalString (cfg.distroId == "nixos") "https://nixos.org/community.html";
30     SUPPORT_END = "2024-06-30";
31     BUG_REPORT_URL = lib.optionalString (cfg.distroId == "nixos") "https://github.com/NixOS/nixpkgs/issues";
32   } // lib.optionalAttrs (cfg.variant_id != null) {
33     VARIANT_ID = cfg.variant_id;
34   };
36   initrdReleaseContents = (removeAttrs osReleaseContents [ "BUILD_ID" ]) // {
37     PRETTY_NAME = "${osReleaseContents.PRETTY_NAME} (Initrd)";
38   };
39   initrdRelease = pkgs.writeText "initrd-release" (attrsToText initrdReleaseContents);
43   imports = [
44     ./label.nix
45     (mkRenamedOptionModule [ "system" "nixosVersion" ] [ "system" "nixos" "version" ])
46     (mkRenamedOptionModule [ "system" "nixosVersionSuffix" ] [ "system" "nixos" "versionSuffix" ])
47     (mkRenamedOptionModule [ "system" "nixosRevision" ] [ "system" "nixos" "revision" ])
48     (mkRenamedOptionModule [ "system" "nixosLabel" ] [ "system" "nixos" "label" ])
49   ];
51   options.boot.initrd.osRelease = mkOption {
52     internal = true;
53     readOnly = true;
54     default = initrdRelease;
55   };
57   options.system = {
59     nixos.version = mkOption {
60       internal = true;
61       type = types.str;
62       description = lib.mdDoc "The full NixOS version (e.g. `16.03.1160.f2d4ee1`).";
63     };
65     nixos.release = mkOption {
66       readOnly = true;
67       type = types.str;
68       default = trivial.release;
69       description = lib.mdDoc "The NixOS release (e.g. `16.03`).";
70     };
72     nixos.versionSuffix = mkOption {
73       internal = true;
74       type = types.str;
75       default = trivial.versionSuffix;
76       description = lib.mdDoc "The NixOS version suffix (e.g. `1160.f2d4ee1`).";
77     };
79     nixos.revision = mkOption {
80       internal = true;
81       type = types.nullOr types.str;
82       default = trivial.revisionWithDefault null;
83       description = lib.mdDoc "The Git revision from which this NixOS configuration was built.";
84     };
86     nixos.codeName = mkOption {
87       readOnly = true;
88       type = types.str;
89       default = trivial.codeName;
90       description = lib.mdDoc "The NixOS release code name (e.g. `Emu`).";
91     };
93     nixos.distroId = mkOption {
94       internal = true;
95       type = types.str;
96       default = "nixos";
97       description = lib.mdDoc "The id of the operating system";
98     };
100     nixos.distroName = mkOption {
101       internal = true;
102       type = types.str;
103       default = "NixOS";
104       description = lib.mdDoc "The name of the operating system";
105     };
107     nixos.variant_id = mkOption {
108       type = types.nullOr (types.strMatching "^[a-z0-9._-]+$");
109       default = null;
110       description = lib.mdDoc "A lower-case string identifying a specific variant or edition of the operating system";
111       example = "installer";
112     };
114     stateVersion = mkOption {
115       type = types.str;
116       # TODO Remove this and drop the default of the option so people are forced to set it.
117       # Doing this also means fixing the comment in nixos/modules/testing/test-instrumentation.nix
118       apply = v:
119         lib.warnIf (options.system.stateVersion.highestPrio == (lib.mkOptionDefault { }).priority)
120           "system.stateVersion is not set, defaulting to ${v}. Read why this matters on https://nixos.org/manual/nixos/stable/options.html#opt-system.stateVersion."
121           v;
122       default = cfg.release;
123       defaultText = literalExpression "config.${opt.release}";
124       description = lib.mdDoc ''
125         This option defines the first version of NixOS you have installed on this particular machine,
126         and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.
128         For example, if NixOS version XX.YY ships with AwesomeDB version N by default, and is then
129         upgraded to version XX.YY+1, which ships AwesomeDB version N+1, the existing databases
130         may no longer be compatible, causing applications to fail, or even leading to data loss.
132         The `stateVersion` mechanism avoids this situation by making the default version of such packages
133         conditional on the first version of NixOS you've installed (encoded in `stateVersion`), instead of
134         simply always using the latest one.
136         Note that this generally only affects applications that can't upgrade their data automatically -
137         applications and services supporting automatic migrations will remain on latest versions when
138         you upgrade.
140         Most users should **never** change this value after the initial install, for any reason,
141         even if you've upgraded your system to a new NixOS release.
143         This value does **not** affect the Nixpkgs version your packages and OS are pulled from,
144         so changing it will **not** upgrade your system.
146         This value being lower than the current NixOS release does **not** mean your system is
147         out of date, out of support, or vulnerable.
149         Do **not** change this value unless you have manually inspected all the changes it would
150         make to your configuration, and migrated your data accordingly.
151       '';
152     };
154     configurationRevision = mkOption {
155       type = types.nullOr types.str;
156       default = null;
157       description = lib.mdDoc "The Git revision of the top-level flake from which this configuration was built.";
158     };
160   };
162   config = {
164     system.nixos = {
165       # These defaults are set here rather than up there so that
166       # changing them would not rebuild the manual
167       version = mkDefault (cfg.release + cfg.versionSuffix);
168     };
170     # Generate /etc/os-release.  See
171     # https://www.freedesktop.org/software/systemd/man/os-release.html for the
172     # format.
173     environment.etc = {
174       "lsb-release".text = attrsToText {
175         LSB_VERSION = "${cfg.release} (${cfg.codeName})";
176         DISTRIB_ID = "${cfg.distroId}";
177         DISTRIB_RELEASE = cfg.release;
178         DISTRIB_CODENAME = toLower cfg.codeName;
179         DISTRIB_DESCRIPTION = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
180       };
182       "os-release".text = attrsToText osReleaseContents;
183     };
185   };
187   # uses version info nixpkgs, which requires a full nixpkgs path
188   meta.buildDocsInSandbox = false;