git.iko.soy/typeable/nixpkgs
on master help sign in
modules: add support for module replacement with disabledModules

This is based on a prototype Nicolas B. Pierron worked on during a discussion we had at FOSDEM. A new version with a workaround for problems of the reverted original. Discussion: https://github.com/NixOS/nixpkgs/commit/3f2566689

commitd88721e4408988202a0ae3cea3f5551ebfa13168 authorDaiderd Jordan <daiderd@gmail.com> dateTue 14 Feb 2017, 23:18 (10 years ago) parentbb9a37a
9 files changed+127−7
lib/modules.nix+17−7 lib/tests/modules.sh+8 lib/tests/modules/default.nix+1 lib/tests/modules/disable-declare-enable.nix+5 lib/tests/modules/disable-define-enable.nix+5 lib/tests/modules/disable-enable-modules.nix+5 nixos/doc/manual/development/replace-modules.xml+75 nixos/doc/manual/development/writing-modules.xml+1 nixos/doc/manual/release-notes/rl-1703.xml+10
lib/modules.nix+17−7
@@ -20,7 +20,8 @@ rec {
2020 , prefix ? []
2121 , # This should only be used for special arguments that need to be evaluated
2222 # when resolving module structure (like in imports). For everything else,
23− # there's _module.args.
23+ # there's _module.args. If specialArgs.modulesPath is defined it will be
24+ # used as the base path for disabledModules.
2425 specialArgs ? {}
2526 , # This would be remove in the future, Prefer _module.args option instead.
2627 args ? {}
@@ -58,10 +59,7 @@ rec {
5859
5960 closed = closeModules (modules ++ [ internalModule ]) ({ inherit config options; lib = import ./.; } // specialArgs);
6061
61− # Note: the list of modules is reversed to maintain backward
62− # compatibility with the old module system. Not sure if this is
63− # the most sensible policy.
64− options = mergeModules prefix (reverseList closed);
62+ options = mergeModules prefix (reverseList (filterModules (specialArgs.modulesPath or "") closed));
6563
6664 # Traverse options and extract the option values into the final
6765 # config set. At the same time, check whether all option
@@ -87,6 +85,16 @@ rec {
8785 result = { inherit options config; };
8886 in result;
8987
88+
89+ # Filter disabled modules. Modules can be disabled allowing
90+ # their implementation to be replaced.
91+ filterModules = modulesPath: modules:
92+ let
93+ moduleKey = m: if isString m then toString modulesPath + "/" + m else toString m;
94+ disabledKeys = map moduleKey (concatMap (m: m.disabledModules) modules);
95+ in
96+ filter (m: !(elem m.key disabledKeys)) modules;
97+
9098 /* Close a set of modules under the ‘imports’ relation. */
9199 closeModules = modules: args:
92100 let
@@ -111,12 +119,13 @@ rec {
111119 else {};
112120 in
113121 if m ? config || m ? options then
114− let badAttrs = removeAttrs m ["imports" "options" "config" "key" "_file" "meta"]; in
122+ let badAttrs = removeAttrs m ["_file" "key" "disabledModules" "imports" "options" "config" "meta"]; in
115123 if badAttrs != {} then
116124 throw "Module `${key}' has an unsupported attribute `${head (attrNames badAttrs)}'. This is caused by assignments to the top-level attributes `config' or `options'."
117125 else
118126 { file = m._file or file;
119127 key = toString m.key or key;
128+ disabledModules = m.disabledModules or [];
120129 imports = m.imports or [];
121130 options = m.options or {};
122131 config = mkMerge [ (m.config or {}) metaSet ];
@@ -124,9 +133,10 @@ rec {
124133 else
125134 { file = m._file or file;
126135 key = toString m.key or key;
136+ disabledModules = m.disabledModules or [];
127137 imports = m.require or [] ++ m.imports or [];
128138 options = {};
129− config = mkMerge [ (removeAttrs m ["key" "_file" "require" "imports"]) metaSet ];
139+ config = mkMerge [ (removeAttrs m ["_file" "key" "disabledModules" "require" "imports"]) metaSet ];
130140 };
131141
132142 applyIfFunction = key: f: args@{ config, options, lib, ... }: if isFunction f then
lib/tests/modules.sh+8
@@ -99,6 +99,14 @@ checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-if-foo-enabl
9999checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-if-enable.nix
100100checkConfigOutput 'true' "$@" ./define-enable.nix ./define-loaOfSub-foo-enable-if.nix
101101
102+# Check disabledModules with config definitions and option declarations.
103+set -- config.enable ./define-enable.nix ./declare-enable.nix
104+checkConfigOutput "true" "$@"
105+checkConfigOutput "false" "$@" ./disable-define-enable.nix
106+checkConfigError "The option .*enable.* defined in .* does not exist" "$@" ./disable-declare-enable.nix
107+checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-define-enable.nix ./disable-declare-enable.nix
108+checkConfigError "attribute .*enable.* in selection path .*config.enable.* not found" "$@" ./disable-enable-modules.nix
109+
102110# Check _module.args.
103111set -- config.enable ./declare-enable.nix ./define-enable-with-custom-arg.nix
104112checkConfigError 'while evaluating the module argument .*custom.* in .*define-enable-with-custom-arg.nix.*:' "$@"
lib/tests/modules/default.nix+1
@@ -3,5 +3,6 @@
33{
44 inherit (lib.evalModules {
55 inherit modules;
6+ specialArgs.modulesPath = ./.;
67 }) config options;
78}
lib/tests/modules/disable-declare-enable.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+{
4+ disabledModules = [ ./declare-enable.nix ];
5+}
lib/tests/modules/disable-define-enable.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+{
4+ disabledModules = [ ./define-enable.nix ];
5+}
lib/tests/modules/disable-enable-modules.nixnew file+5
@@ -0,0 +1,5 @@
1+{ lib, ... }:
2+
3+{
4+ disabledModules = [ "define-enable.nix" "declare-enable.nix" ];
5+}
nixos/doc/manual/development/replace-modules.xmlnew file+75
@@ -0,0 +1,75 @@
1+<section xmlns="http://docbook.org/ns/docbook"
2+ xmlns:xlink="http://www.w3.org/1999/xlink"
3+ xmlns:xi="http://www.w3.org/2001/XInclude"
4+ version="5.0"
5+ xml:id="sec-replace-modules">
6+
7+<title>Replace Modules</title>
8+
9+<para>Modules that are imported can also be disabled. The option
10+ declarations and config implementation of a disabled module will be
11+ ignored, allowing another to take it's place. This can be used to
12+ import a set of modules from another channel while keeping the rest
13+ of the system on a stable release.</para>
14+<para><literal>disabledModules</literal> is a top level attribute like
15+ <literal>imports</literal>, <literal>options</literal> and
16+ <literal>config</literal>. It contains a list of modules that will
17+ be disabled. This can either be the full path to the module or a
18+ string with the filename relative to the modules path
19+ (eg. &lt;nixpkgs/nixos/modules&gt; for nixos).
20+ </para>
21+
22+<para>This example will replace the existing postgresql module with
23+ the version defined in the nixos-unstable channel while keeping the
24+ rest of the modules and packages from the original nixos channel.
25+ This only overrides the module definition, this won't use postgresql
26+ from nixos-unstable unless explicitly configured to do so.</para>
27+
28+<programlisting>
29+{ config, lib, pkgs, ... }:
30+
31+{
32+ disabledModules = [ "services/databases/postgresql.nix" ];
33+
34+ imports =
35+ [ # Use postgresql service from nixos-unstable channel.
36+ # sudo nix-channel --add http://nixos.org/channels/nixos-unstable nixos-unstable
37+ &lt;nixos-unstable/nixos/modules/services/databases/postgresql.nix&gt;
38+ ];
39+
40+ services.postgresql.enable = true;
41+}
42+</programlisting>
43+
44+<para>This example shows how to define a custom module as a
45+ replacement for an existing module. Importing this module will
46+ disable the original module without having to know it's
47+ implementation details.</para>
48+
49+<programlisting>
50+{ config, lib, pkgs, ... }:
51+
52+with lib;
53+
54+let
55+ cfg = config.programs.man;
56+in
57+
58+{
59+ disabledModules = [ "services/programs/man.nix" ];
60+
61+ options = {
62+ programs.man.enable = mkOption {
63+ type = types.bool;
64+ default = true;
65+ description = "Whether to enable manual pages.";
66+ };
67+ };
68+
69+ config = mkIf cfg.enabled {
70+ warnings = [ "disabled manpages for production deployments." ];
71+ };
72+}
73+</programlisting>
74+
75+</section>
nixos/doc/manual/development/writing-modules.xml+1
@@ -179,5 +179,6 @@ in {
179179<xi:include href="option-types.xml" />
180180<xi:include href="option-def.xml" />
181181<xi:include href="meta-attributes.xml" />
182+<xi:include href="replace-modules.xml" />
182183
183184</chapter>
nixos/doc/manual/release-notes/rl-1703.xml+10
@@ -271,6 +271,16 @@ following incompatible changes:</para>
271271 </para>
272272 </listitem>
273273
274+ <listitem>
275+ <para>
276+ Modules can now be disabled by using <link
277+ xlink:href="https://nixos.org/nixpkgs/manual/#sec-replace-modules">
278+ disabledModules</link>, allowing another to take it's place. This can be
279+ used to import a set of modules from another channel while keeping the
280+ rest of the system on a stable release.
281+ </para>
282+ </listitem>
283+
274284</itemizedlist>
275285
276286