lib: Remove global with lib;
This is generally regarded as a code-smell. I did utilize `with lib;` in some places where I felt it aided readability, but it's very clearly scoped in those situations. See https://github.com/nix-community/disko/pull/835#issuecomment-2416126497
This commit is contained in:
@@ -3,9 +3,6 @@
|
|||||||
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
|
, makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
|
||||||
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
|
, eval-config ? import <nixpkgs/nixos/lib/eval-config.nix>
|
||||||
}:
|
}:
|
||||||
with lib;
|
|
||||||
with builtins;
|
|
||||||
|
|
||||||
let
|
let
|
||||||
outputs = import ../default.nix { inherit lib diskoLib; };
|
outputs = import ../default.nix { inherit lib diskoLib; };
|
||||||
diskoLib = {
|
diskoLib = {
|
||||||
@@ -14,9 +11,9 @@ let
|
|||||||
# uses the field "type" to find the correct type in the attrset
|
# uses the field "type" to find the correct type in the attrset
|
||||||
subType = { types, extraArgs ? { parent = { type = "rootNode"; name = "root"; }; } }: lib.mkOptionType {
|
subType = { types, extraArgs ? { parent = { type = "rootNode"; name = "root"; }; } }: lib.mkOptionType {
|
||||||
name = "subType";
|
name = "subType";
|
||||||
description = "one of ${concatStringsSep "," (attrNames types)}";
|
description = "one of ${lib.concatStringsSep "," (lib.attrNames types)}";
|
||||||
check = x: if x ? type then types.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}";
|
check = x: if x ? type then types.${x.type}.check x else throw "No type option set in:\n${lib.generators.toPretty {} x}";
|
||||||
merge = loc: foldl'
|
merge = loc: lib.foldl'
|
||||||
(_res: def: types.${def.value.type}.merge loc [
|
(_res: def: types.${def.value.type}.merge loc [
|
||||||
# we add a dummy root parent node to render documentation
|
# we add a dummy root parent node to render documentation
|
||||||
(lib.recursiveUpdate { value._module.args = extraArgs; } def)
|
(lib.recursiveUpdate { value._module.args = extraArgs; } def)
|
||||||
@@ -53,7 +50,7 @@ let
|
|||||||
deepMergeMap (x: x.t = "test") [ { x = { y = 1; z = 3; }; } { x = { bla = 234; }; } ]
|
deepMergeMap (x: x.t = "test") [ { x = { y = 1; z = 3; }; } { x = { bla = 234; }; } ]
|
||||||
=> { x = { y = 1; z = 3; bla = 234; t = "test"; }; }
|
=> { x = { y = 1; z = 3; bla = 234; t = "test"; }; }
|
||||||
*/
|
*/
|
||||||
deepMergeMap = f: foldr (attr: acc: (recursiveUpdate acc (f attr))) { };
|
deepMergeMap = f: lib.foldr (attr: acc: (lib.recursiveUpdate acc (f attr))) { };
|
||||||
|
|
||||||
/* get a device and an index to get the matching device name
|
/* get a device and an index to get the matching device name
|
||||||
|
|
||||||
@@ -67,6 +64,7 @@ let
|
|||||||
=> "/dev/disk/by-id/xxx-part2"
|
=> "/dev/disk/by-id/xxx-part2"
|
||||||
*/
|
*/
|
||||||
deviceNumbering = dev: index:
|
deviceNumbering = dev: index:
|
||||||
|
let inherit (lib) match; in
|
||||||
if match "/dev/([vs]|(xv)d).+" dev != null then
|
if match "/dev/([vs]|(xv)d).+" dev != null then
|
||||||
dev + toString index # /dev/{s,v,xv}da style
|
dev + toString index # /dev/{s,v,xv}da style
|
||||||
else if match "/dev/(disk|zvol)/.+" dev != null then
|
else if match "/dev/(disk|zvol)/.+" dev != null then
|
||||||
@@ -100,10 +98,10 @@ let
|
|||||||
iter = index: list:
|
iter = index: list:
|
||||||
if list == [ ] then
|
if list == [ ] then
|
||||||
fallback
|
fallback
|
||||||
else if f (head list) then
|
else if f (lib.head list) then
|
||||||
index
|
index
|
||||||
else
|
else
|
||||||
iter (index + 1) (tail list);
|
iter (index + 1) (lib.tail list);
|
||||||
in
|
in
|
||||||
iter 1 list;
|
iter 1 list;
|
||||||
|
|
||||||
@@ -116,7 +114,7 @@ let
|
|||||||
indent "test\nbla"
|
indent "test\nbla"
|
||||||
=> "test\n bla"
|
=> "test\n bla"
|
||||||
*/
|
*/
|
||||||
indent = replaceStrings [ "\n" ] [ "\n " ];
|
indent = lib.replaceStrings [ "\n" ] [ "\n " ];
|
||||||
|
|
||||||
/* A nix option type representing a json datastructure, vendored from nixpkgs to avoid dependency on pkgs */
|
/* A nix option type representing a json datastructure, vendored from nixpkgs to avoid dependency on pkgs */
|
||||||
jsonType =
|
jsonType =
|
||||||
@@ -144,10 +142,10 @@ let
|
|||||||
sortDevicesByDependencies = deviceDependencies: devices:
|
sortDevicesByDependencies = deviceDependencies: devices:
|
||||||
let
|
let
|
||||||
dependsOn = a: b:
|
dependsOn = a: b:
|
||||||
elem a (attrByPath b [ ] deviceDependencies);
|
lib.elem a (lib.attrByPath b [ ] deviceDependencies);
|
||||||
maybeSortedDevices = toposort dependsOn (diskoLib.deviceList devices);
|
maybeSortedDevices = lib.toposort dependsOn (diskoLib.deviceList devices);
|
||||||
in
|
in
|
||||||
if (hasAttr "cycle" maybeSortedDevices) then
|
if (lib.hasAttr "cycle" maybeSortedDevices) then
|
||||||
abort "detected a cycle in your disk setup: ${maybeSortedDevices.cycle}"
|
abort "detected a cycle in your disk setup: ${maybeSortedDevices.cycle}"
|
||||||
else
|
else
|
||||||
maybeSortedDevices.result;
|
maybeSortedDevices.result;
|
||||||
@@ -161,7 +159,7 @@ let
|
|||||||
=> [ [ "zfs" "pool1" ] [ "zfs" "pool2" ] [ "mdadm" "raid1" ] ]
|
=> [ [ "zfs" "pool1" ] [ "zfs" "pool2" ] [ "mdadm" "raid1" ] ]
|
||||||
*/
|
*/
|
||||||
deviceList = devices:
|
deviceList = devices:
|
||||||
concatLists (mapAttrsToList (n: v: (map (x: [ n x ]) (attrNames v))) devices);
|
lib.concatLists (lib.mapAttrsToList (n: v: (map (x: [ n x ]) (lib.attrNames v))) devices);
|
||||||
|
|
||||||
/* Takes either a string or null and returns the string or an empty string
|
/* Takes either a string or null and returns the string or an empty string
|
||||||
|
|
||||||
@@ -173,7 +171,7 @@ let
|
|||||||
maybeSTr "hello world"
|
maybeSTr "hello world"
|
||||||
=> "hello world"
|
=> "hello world"
|
||||||
*/
|
*/
|
||||||
maybeStr = x: optionalString (x != null) x;
|
maybeStr = x: lib.optionalString (x != null) x;
|
||||||
|
|
||||||
/* Takes a Submodules config and options argument and returns a serializable
|
/* Takes a Submodules config and options argument and returns a serializable
|
||||||
subset of config variables as a shell script snippet.
|
subset of config variables as a shell script snippet.
|
||||||
@@ -224,7 +222,7 @@ let
|
|||||||
readOnly = true;
|
readOnly = true;
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
default = ''
|
default = ''
|
||||||
( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]} #
|
( # ${config.type} ${lib.concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]} #
|
||||||
${diskoLib.indent (diskoLib.defineHookVariables { inherit options; })}
|
${diskoLib.indent (diskoLib.defineHookVariables { inherit options; })}
|
||||||
${diskoLib.indent config.preCreateHook}
|
${diskoLib.indent config.preCreateHook}
|
||||||
${diskoLib.indent attrs.default}
|
${diskoLib.indent attrs.default}
|
||||||
@@ -309,29 +307,29 @@ let
|
|||||||
optionTypes = rec {
|
optionTypes = rec {
|
||||||
filename = lib.mkOptionType {
|
filename = lib.mkOptionType {
|
||||||
name = "filename";
|
name = "filename";
|
||||||
check = isString;
|
check = lib.isString;
|
||||||
merge = mergeOneOption;
|
merge = lib.mergeOneOption;
|
||||||
description = "A filename";
|
description = "A filename";
|
||||||
};
|
};
|
||||||
|
|
||||||
absolute-pathname = lib.mkOptionType {
|
absolute-pathname = lib.mkOptionType {
|
||||||
name = "absolute pathname";
|
name = "absolute pathname";
|
||||||
check = x: isString x && substring 0 1 x == "/" && pathname.check x;
|
check = x: lib.isString x && lib.substring 0 1 x == "/" && pathname.check x;
|
||||||
merge = mergeOneOption;
|
merge = lib.mergeOneOption;
|
||||||
description = "An absolute path";
|
description = "An absolute path";
|
||||||
};
|
};
|
||||||
|
|
||||||
pathname = lib.mkOptionType {
|
pathname = lib.mkOptionType {
|
||||||
name = "pathname";
|
name = "pathname";
|
||||||
check = x:
|
check = x:
|
||||||
let
|
with lib; let
|
||||||
# The filter is used to normalize paths, i.e. to remove duplicated and
|
# The filter is used to normalize paths, i.e. to remove duplicated and
|
||||||
# trailing slashes. It also removes leading slashes, thus we have to
|
# trailing slashes. It also removes leading slashes, thus we have to
|
||||||
# check for "/" explicitly below.
|
# check for "/" explicitly below.
|
||||||
xs = filter (s: stringLength s > 0) (splitString "/" x);
|
xs = filter (s: stringLength s > 0) (splitString "/" x);
|
||||||
in
|
in
|
||||||
isString x && (x == "/" || (length xs > 0 && all filename.check xs));
|
isString x && (x == "/" || (length xs > 0 && all filename.check xs));
|
||||||
merge = mergeOneOption;
|
merge = lib.mergeOneOption;
|
||||||
description = "A path name";
|
description = "A path name";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -375,7 +373,7 @@ let
|
|||||||
meta informationen generated by disko
|
meta informationen generated by disko
|
||||||
currently used for building a dependency list so we know in which order to create the devices
|
currently used for building a dependency list so we know in which order to create the devices
|
||||||
'';
|
'';
|
||||||
default = diskoLib.deepMergeMap (dev: dev._meta) (flatten (map attrValues (attrValues devices)));
|
default = diskoLib.deepMergeMap (dev: dev._meta) (lib.flatten (map lib.attrValues (lib.attrValues devices)));
|
||||||
};
|
};
|
||||||
_packages = lib.mkOption {
|
_packages = lib.mkOption {
|
||||||
internal = true;
|
internal = true;
|
||||||
@@ -383,7 +381,7 @@ let
|
|||||||
packages required by the disko configuration
|
packages required by the disko configuration
|
||||||
coreutils is always included
|
coreutils is always included
|
||||||
'';
|
'';
|
||||||
default = pkgs: unique ((flatten (map (dev: dev._pkgs pkgs) (flatten (map attrValues (attrValues devices))))) ++ [ pkgs.coreutils-full ]);
|
default = pkgs: with lib; unique ((flatten (map (dev: dev._pkgs pkgs) (flatten (map attrValues (attrValues devices))))) ++ [ pkgs.coreutils-full ]);
|
||||||
};
|
};
|
||||||
_scripts = lib.mkOption {
|
_scripts = lib.mkOption {
|
||||||
internal = true;
|
internal = true;
|
||||||
@@ -393,7 +391,10 @@ let
|
|||||||
default = { pkgs, checked ? false }:
|
default = { pkgs, checked ? false }:
|
||||||
let
|
let
|
||||||
throwIfNoDisksDetected = _: v: if devices.disk == { } then throw "No disks defined, did you forget to import your disko config?" else v;
|
throwIfNoDisksDetected = _: v: if devices.disk == { } then throw "No disks defined, did you forget to import your disko config?" else v;
|
||||||
destroyDependencies = with pkgs; [
|
in
|
||||||
|
lib.mapAttrs throwIfNoDisksDetected {
|
||||||
|
destroyScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-destroy" ''
|
||||||
|
export PATH=${lib.makeBinPath (with pkgs; [
|
||||||
util-linux
|
util-linux
|
||||||
e2fsprogs
|
e2fsprogs
|
||||||
mdadm
|
mdadm
|
||||||
@@ -404,11 +405,7 @@ let
|
|||||||
gnused
|
gnused
|
||||||
gawk
|
gawk
|
||||||
coreutils-full
|
coreutils-full
|
||||||
];
|
])}:$PATH
|
||||||
in
|
|
||||||
mapAttrs throwIfNoDisksDetected {
|
|
||||||
destroyScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko-destroy" ''
|
|
||||||
export PATH=${lib.makeBinPath destroyDependencies}:$PATH
|
|
||||||
${cfg.config._destroy}
|
${cfg.config._destroy}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
@@ -423,7 +420,7 @@ let
|
|||||||
'';
|
'';
|
||||||
|
|
||||||
diskoScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko" ''
|
diskoScript = (diskoLib.writeCheckedBash { inherit pkgs checked; }) "disko" ''
|
||||||
export PATH=${lib.makeBinPath ((cfg.config._packages pkgs) ++ [ pkgs.bash ] ++ destroyDependencies)}:$PATH
|
export PATH=${lib.makeBinPath ((cfg.config._packages pkgs) ++ [ pkgs.bash ])}:$PATH
|
||||||
${cfg.config._disko}
|
${cfg.config._disko}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
@@ -467,7 +464,7 @@ let
|
|||||||
The script to create all devices defined by disko.devices
|
The script to create all devices defined by disko.devices
|
||||||
'';
|
'';
|
||||||
default =
|
default =
|
||||||
let
|
with lib; let
|
||||||
sortedDeviceList = diskoLib.sortDevicesByDependencies (cfg.config._meta.deviceDependencies or { }) devices;
|
sortedDeviceList = diskoLib.sortDevicesByDependencies (cfg.config._meta.deviceDependencies or { }) devices;
|
||||||
in
|
in
|
||||||
''
|
''
|
||||||
@@ -487,7 +484,7 @@ let
|
|||||||
The script to mount all devices defined by disko.devices
|
The script to mount all devices defined by disko.devices
|
||||||
'';
|
'';
|
||||||
default =
|
default =
|
||||||
let
|
with lib; let
|
||||||
fsMounts = diskoLib.deepMergeMap (dev: dev._mount.fs or { }) (flatten (map attrValues (attrValues devices)));
|
fsMounts = diskoLib.deepMergeMap (dev: dev._mount.fs or { }) (flatten (map attrValues (attrValues devices)));
|
||||||
sortedDeviceList = diskoLib.sortDevicesByDependencies (cfg.config._meta.deviceDependencies or { }) devices;
|
sortedDeviceList = diskoLib.sortDevicesByDependencies (cfg.config._meta.deviceDependencies or { }) devices;
|
||||||
in
|
in
|
||||||
@@ -518,11 +515,11 @@ let
|
|||||||
The NixOS config generated by disko
|
The NixOS config generated by disko
|
||||||
'';
|
'';
|
||||||
default =
|
default =
|
||||||
let
|
with lib; let
|
||||||
configKeys = flatten (map attrNames (flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))))));
|
configKeys = flatten (map attrNames (flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))))));
|
||||||
collectedConfigs = flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))));
|
collectedConfigs = flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))));
|
||||||
in
|
in
|
||||||
lib.genAttrs configKeys (key: lib.mkMerge (lib.catAttrs key collectedConfigs));
|
genAttrs configKeys (key: mkMerge (catAttrs key collectedConfigs));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user