summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-30 13:36:57 +0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-30 13:44:50 +0200
commitf463d2490322a7a7e41634bbf23fc9cbdbdb9fae (patch)
tree3a7c53aeccc5d075999b34264db969d519815fcd
parentd61e1b24e6d280ab596abca0da2b33293eaaeef6 (diff)
downloadnixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar.gz
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar.bz2
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar.lz
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar.xz
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.tar.zst
nixpkgs-f463d2490322a7a7e41634bbf23fc9cbdbdb9fae.zip
Add read-only options
These are options that can have only one definition, regardless of
priority.
-rw-r--r--lib/modules.nix11
-rw-r--r--lib/options.nix2
-rw-r--r--nixos/doc/manual/options-to-docbook.xsl4
-rw-r--r--nixos/modules/misc/version.nix4
4 files changed, 16 insertions, 5 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index d7037abfbef..50827e18f10 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -261,11 +261,16 @@ rec {
   evalOptionValue = loc: opt: defs:
     let
       # Add in the default value for this option, if any.
-      defs' = (optional (opt ? default)
-        { file = head opt.declarations; value = mkOptionDefault opt.default; }) ++ defs;
+      defs' =
+          (optional (opt ? default)
+            { file = head opt.declarations; value = mkOptionDefault opt.default; }) ++ defs;
 
       # Handle properties, check types, and merge everything together.
-      res = mergeDefinitions loc opt.type defs';
+      res =
+        if opt.readOnly or false && length defs' > 1 then
+          throw "The option `${showOption loc}' is read-only, but it's set multiple times."
+        else
+          mergeDefinitions loc opt.type defs';
 
       # Check whether the option is defined, and apply the ‘apply’
       # function to the merged value.  This allows options to yield a
diff --git a/lib/options.nix b/lib/options.nix
index 5c543f56bcf..444ec37e6ea 100644
--- a/lib/options.nix
+++ b/lib/options.nix
@@ -19,6 +19,7 @@ rec {
     , apply ? null # Function that converts the option value to something else.
     , internal ? null # Whether the option is for NixOS developers only.
     , visible ? null # Whether the option shows up in the manual.
+    , readOnly ? null # Whether the option can be set only once
     , options ? null # Obsolete, used by types.optionSet.
     } @ attrs:
     attrs // { _type = "option"; };
@@ -90,6 +91,7 @@ rec {
           declarations = filter (x: x != unknownModule) opt.declarations;
           internal = opt.internal or false;
           visible = opt.visible or true;
+          readOnly = opt.readOnly or false;
           type = opt.type.name or null;
         }
         // (if opt ? example then { example = scrubOptionValue opt.example; } else {})
diff --git a/nixos/doc/manual/options-to-docbook.xsl b/nixos/doc/manual/options-to-docbook.xsl
index f588a019e63..3d6ea942701 100644
--- a/nixos/doc/manual/options-to-docbook.xsl
+++ b/nixos/doc/manual/options-to-docbook.xsl
@@ -39,6 +39,10 @@
                   <emphasis>Type:</emphasis>
                   <xsl:text> </xsl:text>
                   <xsl:apply-templates select="attr[@name = 'type']" mode="top" />
+                  <xsl:if test="attr[@name = 'readOnly']/bool/@value = 'true'">
+                    <xsl:text> </xsl:text>
+                    <emphasis>(read only)</emphasis>
+                  </xsl:if>
                 </para>
               </xsl:if>
 
diff --git a/nixos/modules/misc/version.nix b/nixos/modules/misc/version.nix
index 8a52df42dd8..dc8eb4e8832 100644
--- a/nixos/modules/misc/version.nix
+++ b/nixos/modules/misc/version.nix
@@ -29,7 +29,7 @@ with lib;
     };
 
     system.nixosRelease = mkOption {
-      internal = true;
+      readOnly = true;
       type = types.str;
       default = readFile "${toString pkgs.path}/.version";
       description = "NixOS release.";
@@ -48,7 +48,7 @@ with lib;
     };
 
     system.nixosCodeName = mkOption {
-      internal = true;
+      readOnly = true;
       type = types.str;
       description = "NixOS release code name.";
     };