Skip to content

Commit

Permalink
nixos/specialisation.nix: Extract module
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Dec 9, 2021
1 parent 8da928c commit 2bf147c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 60 deletions.
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@
./services/x11/xserver.nix
./system/activation/activation-script.nix
./system/activation/bootable.nix
./system/activation/specialisation.nix
./system/activation/system-with-activation.nix
./system/activation/top-level.nix
./system/boot/binfmt.nix
Expand Down
81 changes: 81 additions & 0 deletions nixos/modules/system/activation/specialisation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{ config, lib, pkgs, extendModules, noUserModules, ... }:

let
inherit (lib)
concatStringsSep
mapAttrs
mapAttrsToList
mkOption
types
;

# This attribute is responsible for creating boot entries for
# child configuration. They are only (directly) accessible
# when the parent configuration is boot default. For example,
# you can provide an easy way to boot the same configuration
# as you use, but with another kernel
# !!! fix this
children =
mapAttrs
(childName: childConfig: childConfig.configuration.system.build.toplevel)
config.specialisation;

in
{
config = {
system.systemBuilderCommands = lib.mkIf (config.specialisation != { }) ''
mkdir $out/specialisation
${concatStringsSep "\n"
(mapAttrsToList (name: path: "ln -s ${path} $out/specialisation/${name}") children)}
'';
};

options = {

specialisation = mkOption {
default = { };
example = lib.literalExpression "{ fewJobsManyCores.configuration = { nix.buildCores = 0; nix.maxJobs = 1; }; }";
description = ''
Additional configurations to build. If
<literal>inheritParentConfig</literal> is true, the system
will be based on the overall system configuration.
To switch to a specialised configuration
(e.g. <literal>fewJobsManyCores</literal>) at runtime, run:
<screen>
<prompt># </prompt>sudo /run/current-system/specialisation/fewJobsManyCores/bin/switch-to-configuration test
</screen>
'';
type = types.attrsOf (types.submodule (
local@{ ... }:
let
extend =
if local.config.inheritParentConfig
then extendModules
else noUserModules.extendModules;
in
{
options.inheritParentConfig = mkOption {
type = types.bool;
default = true;
description = "Include the entire system's configuration. Set to false to make a completely differently configured system.";
};

options.configuration = mkOption {
default = { };
description = ''
Arbitrary NixOS configuration.
Anything you can add to a normal NixOS configuration, you can add
here, including imports and config values, although nested
specialisations will be ignored.
'';
visible = "shallow";
inherit (extend { modules = [ ./no-clone.nix ]; }) type;
};
}
));
};
};
}
60 changes: 0 additions & 60 deletions nixos/modules/system/activation/top-level.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
with lib;

let


# This attribute is responsible for creating boot entries for
# child configuration. They are only (directly) accessible
# when the parent configuration is boot default. For example,
# you can provide an easy way to boot the same configuration
# as you use, but with another kernel
# !!! fix this
children =
mapAttrs
(childName: childConfig: childConfig.configuration.system.build.toplevel)
config.specialisation;

systemBuilder =
''
mkdir $out
Expand All @@ -31,10 +18,6 @@ let
echo -n "systemd ${toString config.systemd.package.interfaceVersion}" > $out/init-interface-version
echo -n "$nixosLabel" > $out/nixos-version
mkdir $out/specialisation
${concatStringsSep "\n"
(mapAttrsToList (name: path: "ln -s ${path} $out/specialisation/${name}") children)}
echo -n "${toString config.system.extraDependencies}" > $out/extra-dependencies
${config.system.systemBuilderCommands}
Expand Down Expand Up @@ -95,49 +78,6 @@ in

options = {

specialisation = mkOption {
default = {};
example = lib.literalExpression "{ fewJobsManyCores.configuration = { nix.buildCores = 0; nix.maxJobs = 1; }; }";
description = ''
Additional configurations to build. If
<literal>inheritParentConfig</literal> is true, the system
will be based on the overall system configuration.
To switch to a specialised configuration
(e.g. <literal>fewJobsManyCores</literal>) at runtime, run:
<screen>
<prompt># </prompt>sudo /run/current-system/specialisation/fewJobsManyCores/bin/switch-to-configuration test
</screen>
'';
type = types.attrsOf (types.submodule (
local@{ ... }: let
extend = if local.config.inheritParentConfig
then extendModules
else noUserModules.extendModules;
in {
options.inheritParentConfig = mkOption {
type = types.bool;
default = true;
description = "Include the entire system's configuration. Set to false to make a completely differently configured system.";
};

options.configuration = mkOption {
default = {};
description = ''
Arbitrary NixOS configuration.
Anything you can add to a normal NixOS configuration, you can add
here, including imports and config values, although nested
specialisations will be ignored.
'';
visible = "shallow";
inherit (extend { modules = [ ./no-clone.nix ]; }) type;
};
})
);
};

system.copySystemConfiguration = mkOption {
type = types.bool;
default = false;
Expand Down

0 comments on commit 2bf147c

Please sign in to comment.