about summary refs log tree commit diff
path: root/nixos/modules/misc/version.nix
blob: f77261b162a6e1427b72fbf8e145e50d658f2847 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{ config, lib, options, pkgs, ... }:

let
  cfg = config.system.nixos;
  opt = options.system.nixos;

  inherit (lib)
    concatStringsSep mapAttrsToList toLower optionalString
    literalExpression mkRenamedOptionModule mkDefault mkOption trivial types;

  needsEscaping = s: null != builtins.match "[a-zA-Z0-9]+" s;
  escapeIfNecessary = s: if needsEscaping s then s else ''"${lib.escape [ "\$" "\"" "\\" "\`" ] s}"'';
  attrsToText = attrs:
    concatStringsSep "\n"
      (mapAttrsToList (n: v: ''${n}=${escapeIfNecessary (toString v)}'') attrs)
    + "\n";

  osReleaseContents =
    let
      isNixos = cfg.distroId == "nixos";
    in
    {
      NAME = "${cfg.distroName}";
      ID = "${cfg.distroId}";
      ID_LIKE = optionalString (!isNixos) "nixos";
      VENDOR_NAME = cfg.vendorName;
      VERSION = "${cfg.release} (${cfg.codeName})";
      VERSION_CODENAME = toLower cfg.codeName;
      VERSION_ID = cfg.release;
      BUILD_ID = cfg.version;
      PRETTY_NAME = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
      CPE_NAME = "cpe:/o:${cfg.vendorId}:${cfg.distroId}:${cfg.release}";
      LOGO = "nix-snowflake";
      HOME_URL = optionalString isNixos "https://nixos.org/";
      VENDOR_URL = optionalString isNixos "https://nixos.org/";
      DOCUMENTATION_URL = optionalString isNixos "https://nixos.org/learn.html";
      SUPPORT_URL = optionalString isNixos "https://nixos.org/community.html";
      BUG_REPORT_URL = optionalString isNixos "https://github.com/NixOS/nixpkgs/issues";
      ANSI_COLOR = optionalString isNixos "1;34";
      IMAGE_ID = optionalString (config.system.image.id != null) config.system.image.id;
      IMAGE_VERSION = optionalString (config.system.image.version != null) config.system.image.version;
      VARIANT = optionalString (cfg.variantName != null) cfg.variantName;
      VARIANT_ID = optionalString (cfg.variant_id != null) cfg.variant_id;
      DEFAULT_HOSTNAME = config.networking.fqdnOrHostName;
      SUPPORT_END = "2025-06-30";
    };

  initrdReleaseContents = (removeAttrs osReleaseContents [ "BUILD_ID" ]) // {
    PRETTY_NAME = "${osReleaseContents.PRETTY_NAME} (Initrd)";
  };
  initrdRelease = pkgs.writeText "initrd-release" (attrsToText initrdReleaseContents);

in
{
  imports = [
    ./label.nix
    (mkRenamedOptionModule [ "system" "nixosVersion" ] [ "system" "nixos" "version" ])
    (mkRenamedOptionModule [ "system" "nixosVersionSuffix" ] [ "system" "nixos" "versionSuffix" ])
    (mkRenamedOptionModule [ "system" "nixosRevision" ] [ "system" "nixos" "revision" ])
    (mkRenamedOptionModule [ "system" "nixosLabel" ] [ "system" "nixos" "label" ])
  ];

  options.boot.initrd.osRelease = mkOption {
    internal = true;
    readOnly = true;
    default = initrdRelease;
  };

  options.system = {
    nixos = {
      version = mkOption {
        internal = true;
        type = types.str;
        description = "The full NixOS version (e.g. `16.03.1160.f2d4ee1`).";
      };

      release = mkOption {
        readOnly = true;
        type = types.str;
        default = trivial.release;
        description = "The NixOS release (e.g. `16.03`).";
      };

      versionSuffix = mkOption {
        internal = true;
        type = types.str;
        default = trivial.versionSuffix;
        description = "The NixOS version suffix (e.g. `1160.f2d4ee1`).";
      };

      revision = mkOption {
        internal = true;
        type = types.nullOr types.str;
        default = trivial.revisionWithDefault null;
        description = "The Git revision from which this NixOS configuration was built.";
      };

      codeName = mkOption {
        readOnly = true;
        type = types.str;
        default = trivial.codeName;
        description = "The NixOS release code name (e.g. `Emu`).";
      };

      distroId = mkOption {
        internal = true;
        type = types.str;
        default = "nixos";
        description = "The id of the operating system";
      };

      distroName = mkOption {
        internal = true;
        type = types.str;
        default = "NixOS";
        description = "The name of the operating system";
      };

      variant_id = mkOption {
        type = types.nullOr (types.strMatching "^[a-z0-9._-]+$");
        default = null;
        description = "A lower-case string identifying a specific variant or edition of the operating system";
        example = "installer";
      };

      variantName = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = "A string identifying a specific variant or edition of the operating system suitable for presentation to the user";
        example = "NixOS Installer Image";
      };

      vendorId = mkOption {
        internal = true;
        type = types.str;
        default = "nixos";
        description = "The id of the operating system vendor";
      };

      vendorName = mkOption {
        internal = true;
        type = types.str;
        default = "NixOS";
        description = "The name of the operating system vendor";
      };
    };

    image = {

      id = lib.mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          Image identifier.

          This corresponds to the IMAGE_ID field in os-release. See the
          upstream docs for more details on valid characters for this field:
          https://www.freedesktop.org/software/systemd/man/latest/os-release.html#IMAGE_ID=

          You would only want to set this option if you're build NixOS appliance images.
        '';
      };

      version = lib.mkOption {
        type = types.nullOr types.str;
        default = null;
        description = ''
          Image version.

          This corresponds to the IMAGE_VERSION field in os-release. See the
          upstream docs for more details on valid characters for this field:
          https://www.freedesktop.org/software/systemd/man/latest/os-release.html#IMAGE_VERSION=

          You would only want to set this option if you're build NixOS appliance images.
        '';
      };

    };

    stateVersion = mkOption {
      type = types.str;
      # TODO Remove this and drop the default of the option so people are forced to set it.
      # Doing this also means fixing the comment in nixos/modules/testing/test-instrumentation.nix
      apply = v:
        lib.warnIf (options.system.stateVersion.highestPrio == (lib.mkOptionDefault { }).priority)
          "system.stateVersion is not set, defaulting to ${v}. Read why this matters on https://nixos.org/manual/nixos/stable/options.html#opt-system.stateVersion."
          v;
      default = cfg.release;
      defaultText = literalExpression "config.${opt.release}";
      description = ''
        This option defines the first version of NixOS you have installed on this particular machine,
        and is used to maintain compatibility with application data (e.g. databases) created on older NixOS versions.

        For example, if NixOS version XX.YY ships with AwesomeDB version N by default, and is then
        upgraded to version XX.YY+1, which ships AwesomeDB version N+1, the existing databases
        may no longer be compatible, causing applications to fail, or even leading to data loss.

        The `stateVersion` mechanism avoids this situation by making the default version of such packages
        conditional on the first version of NixOS you've installed (encoded in `stateVersion`), instead of
        simply always using the latest one.

        Note that this generally only affects applications that can't upgrade their data automatically -
        applications and services supporting automatic migrations will remain on latest versions when
        you upgrade.

        Most users should **never** change this value after the initial install, for any reason,
        even if you've upgraded your system to a new NixOS release.

        This value does **not** affect the Nixpkgs version your packages and OS are pulled from,
        so changing it will **not** upgrade your system.

        This value being lower than the current NixOS release does **not** mean your system is
        out of date, out of support, or vulnerable.

        Do **not** change this value unless you have manually inspected all the changes it would
        make to your configuration, and migrated your data accordingly.
      '';
    };

    configurationRevision = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = "The Git revision of the top-level flake from which this configuration was built.";
    };

  };

  config = {

    system.nixos = {
      # These defaults are set here rather than up there so that
      # changing them would not rebuild the manual
      version = mkDefault (cfg.release + cfg.versionSuffix);
    };

    # Generate /etc/os-release.  See
    # https://www.freedesktop.org/software/systemd/man/os-release.html for the
    # format.
    environment.etc = {
      "lsb-release".text = attrsToText {
        LSB_VERSION = "${cfg.release} (${cfg.codeName})";
        DISTRIB_ID = "${cfg.distroId}";
        DISTRIB_RELEASE = cfg.release;
        DISTRIB_CODENAME = toLower cfg.codeName;
        DISTRIB_DESCRIPTION = "${cfg.distroName} ${cfg.release} (${cfg.codeName})";
      };

      "os-release".text = attrsToText osReleaseContents;
    };

  };

  # uses version info nixpkgs, which requires a full nixpkgs path
  meta.buildDocsInSandbox = false;
}