style: Apply nixpkgs-fmt and fix

Apply standard formatting and some statix conventions using;

```sh
nixpkgs-fmt **.nix && statix fix .
```

With the intent of making contribution a bit easier and reducing mental
load in hand formatting (in the same vein as [black]).

[black]: https://github.com/psf/black#the-uncompromising-code-formatter
This commit is contained in:
Chris Scutcher 2023-02-06 14:24:34 +00:00
parent b1a4ecb8ca
commit 08435eec4b
No known key found for this signature in database
GPG key ID: 19C0BED130CD39CE
23 changed files with 171 additions and 158 deletions

5
ci.nix
View file

@ -1,6 +1,7 @@
let let
pkgs = import <nixpkgs> {}; pkgs = import <nixpkgs> { };
in { in
{
test = pkgs.writeScript "test" '' test = pkgs.writeScript "test" ''
#!/bin/sh #!/bin/sh
nix-build "${toString ./tests}"; nix-build "${toString ./tests}";

52
cli.nix
View file

@ -1,4 +1,4 @@
{ pkgs ? import <nixpkgs> {} { pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib , lib ? pkgs.lib
, mode ? "mount" , mode ? "mount"
, flake ? null , flake ? null
@ -6,35 +6,39 @@
, diskoFile ? null , diskoFile ? null
, rootMountPoint ? "/mnt" , rootMountPoint ? "/mnt"
, noDeps ? false , noDeps ? false
, ... }@args: , ...
}@args:
let let
disko = import ./. { disko = import ./. {
inherit rootMountPoint; inherit rootMountPoint;
inherit lib; inherit lib;
}; };
diskFormat = if flake != null then diskFormat =
(pkgs.lib.attrByPath [ "diskoConfigurations" flakeAttr ] (builtins.abort "${flakeAttr} does not exist") (builtins.getFlake flake)) args if flake != null then
else (pkgs.lib.attrByPath [ "diskoConfigurations" flakeAttr ] (builtins.abort "${flakeAttr} does not exist") (builtins.getFlake flake)) args
import diskoFile ({ inherit lib; } // args); else
import diskoFile ({ inherit lib; } // args);
diskoEval = if noDeps then diskoEval =
if (mode == "create") then if noDeps then
disko.createScriptNoDeps diskFormat pkgs if (mode == "create") then
else if (mode == "mount") then disko.createScriptNoDeps diskFormat pkgs
disko.mountScriptNoDeps diskFormat pkgs else if (mode == "mount") then
else if (mode == "zap_create_mount") then disko.mountScriptNoDeps diskFormat pkgs
disko.zapCreateMountScriptNoDeps diskFormat pkgs else if (mode == "zap_create_mount") then
disko.zapCreateMountScriptNoDeps diskFormat pkgs
else
builtins.abort "invalid mode"
else else
builtins.abort "invalid mode" if (mode == "create") then
else disko.createScript diskFormat pkgs
if (mode == "create") then else if (mode == "mount") then
disko.createScript diskFormat pkgs disko.mountScript diskFormat pkgs
else if (mode == "mount") then else if (mode == "zap_create_mount") then
disko.mountScript diskFormat pkgs disko.zapCreateMountScript diskFormat pkgs
else if (mode == "zap_create_mount") then else
disko.zapCreateMountScript diskFormat pkgs builtins.abort "invalid mode"
else
builtins.abort "invalid mode"
; ;
in diskoEval in
diskoEval

View file

@ -14,8 +14,9 @@ let
}; };
}; };
}; };
in { in
types = types; {
inherit types;
create = cfg: types.diskoLib.create (eval cfg).config.devices; create = cfg: types.diskoLib.create (eval cfg).config.devices;
createScript = cfg: pkgs: pkgs.writeScript "disko-create" '' createScript = cfg: pkgs: pkgs.writeScript "disko-create" ''
#!/usr/bin/env bash #!/usr/bin/env bash

13
doc.nix
View file

@ -6,12 +6,12 @@ let
rootMountPoint = "/mnt"; rootMountPoint = "/mnt";
}; };
eval = lib.evalModules { eval = lib.evalModules {
modules = [ modules = [
{ {
options.disko = { options.disko = {
devices = lib.mkOption { devices = lib.mkOption {
type = types.devices; type = types.devices;
default = {}; default = { };
description = "The devices to set up"; description = "The devices to set up";
}; };
}; };
@ -19,7 +19,7 @@ let
]; ];
}; };
options = nixosOptionsDoc { options = nixosOptionsDoc {
options = eval.options; inherit (eval) options;
}; };
md = (runCommand "disko-options.md" { } '' md = (runCommand "disko-options.md" { } ''
cat >$out <<EOF cat >$out <<EOF
@ -28,14 +28,15 @@ let
EOF EOF
cat ${options.optionsCommonMark} >>$out cat ${options.optionsCommonMark} >>$out
'').overrideAttrs (o: { '').overrideAttrs (o: {
# Work around https://github.com/hercules-ci/hercules-ci-agent/issues/168 # Work around https://github.com/hercules-ci/hercules-ci-agent/issues/168
allowSubstitutes = true; allowSubstitutes = true;
}); });
css = fetchurl { css = fetchurl {
url = "https://gist.githubusercontent.com/killercup/5917178/raw/40840de5352083adb2693dc742e9f75dbb18650f/pandoc.css"; url = "https://gist.githubusercontent.com/killercup/5917178/raw/40840de5352083adb2693dc742e9f75dbb18650f/pandoc.css";
sha256 = "sha256-SzSvxBIrylxBF6B/mOImLlZ+GvCfpWNLzGFViLyOeTk="; sha256 = "sha256-SzSvxBIrylxBF6B/mOImLlZ+GvCfpWNLzGFViLyOeTk=";
}; };
in runCommand "disko.html" { nativeBuildInputs = [ pandoc ]; } '' in
runCommand "disko.html" { nativeBuildInputs = [ pandoc ]; } ''
mkdir $out mkdir $out
cp ${css} $out/pandoc.css cp ${css} $out/pandoc.css
pandoc --css="pandoc.css" ${md} --to=html5 -s -f markdown+smart --metadata pagetitle="Disko options" -o $out/index.html pandoc --css="pandoc.css" ${md} --to=html5 -s -f markdown+smart --metadata pagetitle="Disko options" -o $out/index.html

View file

@ -1,7 +1,7 @@
# Example to create a bios compatible gpt partition # Example to create a bios compatible gpt partition
{ disks ? [ "/dev/vdb" ], lib, ... }: { { disks ? [ "/dev/vdb" ], lib, ... }: {
disk = lib.genAttrs [ (lib.head disks) ] (device: { disk = lib.genAttrs [ (lib.head disks) ] (device: {
device = device; inherit device;
type = "disk"; type = "disk";
content = { content = {
type = "table"; type = "table";

View file

@ -6,49 +6,56 @@
#inputs.nixpkgs.url = "nixpkgs"; #inputs.nixpkgs.url = "nixpkgs";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs, ... }: let outputs = { self, nixpkgs, ... }:
supportedSystems = [ let
"x86_64-linux" supportedSystems = [
"i686-linux" "x86_64-linux"
"aarch64-linux" "i686-linux"
]; "aarch64-linux"
forAllSystems = nixpkgs.lib.genAttrs supportedSystems; ];
in { forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
nixosModules.disko = import ./module.nix;
lib = import ./. {
inherit (nixpkgs) lib;
};
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
disko = pkgs.callPackage ./package.nix {};
disko-doc = pkgs.callPackage ./doc.nix { };
default = self.packages.${system}.disko;
# The way bcachefs support is maintained in nixpkgs is prone to breakage.
# That's why we need to maintain a fork here:
# https://github.com/NixOS/nixpkgs/issues/212086
linux-bcachefs = pkgs.callPackage ./linux-testing-bcachefs.nix {};
});
legacyPackages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
linuxPackages_bcachefs = pkgs.linuxPackagesFor self.packages.${pkgs.system}.linux-bcachefs;
});
# TODO: disable bios-related tests on aarch64...
# Run checks: nix flake check -L
checks = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
nixosTests = import ./tests {
inherit pkgs;
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
};
shellcheck = pkgs.runCommand "shellcheck" { nativeBuildInputs = [ pkgs.shellcheck ]; } ''
cd ${./.}
shellcheck disk-deactivate/disk-deactivate disko
touch $out
'';
in in
nixosTests // { inherit shellcheck; }); {
}; nixosModules.disko = import ./module.nix;
lib = import ./. {
inherit (nixpkgs) lib;
};
packages = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
disko = pkgs.callPackage ./package.nix { };
disko-doc = pkgs.callPackage ./doc.nix { };
default = self.packages.${system}.disko;
# The way bcachefs support is maintained in nixpkgs is prone to breakage.
# That's why we need to maintain a fork here:
# https://github.com/NixOS/nixpkgs/issues/212086
linux-bcachefs = pkgs.callPackage ./linux-testing-bcachefs.nix { };
});
legacyPackages = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
linuxPackages_bcachefs = pkgs.linuxPackagesFor self.packages.${pkgs.system}.linux-bcachefs;
});
# TODO: disable bios-related tests on aarch64...
# Run checks: nix flake check -L
checks = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
nixosTests = import ./tests {
inherit pkgs;
makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix");
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
};
shellcheck = pkgs.runCommand "shellcheck" { nativeBuildInputs = [ pkgs.shellcheck ]; } ''
cd ${./.}
shellcheck disk-deactivate/disk-deactivate disko
touch $out
'';
in
nixosTests // { inherit shellcheck; });
};
} }

View file

@ -18,11 +18,11 @@ buildLinux (args // {
sha256 = "sha256-n00qPtHHEHt3FSIRMoP9IJFAdQJNNwabg+WAKppSAS8="; sha256 = "sha256-n00qPtHHEHt3FSIRMoP9IJFAdQJNNwabg+WAKppSAS8=";
}; };
kernelPatches = (args.kernelPatches or []) ++ [{ kernelPatches = (args.kernelPatches or [ ]) ++ [{
name = "bcachefs-config"; name = "bcachefs-config";
patch = null; patch = null;
extraConfig = '' extraConfig = ''
BCACHEFS_FS m BCACHEFS_FS m
''; '';
}]; }];
} // (args.argsOverride or {})) } // (args.argsOverride or { }))

View file

@ -2,14 +2,15 @@
let let
types = import ./types { types = import ./types {
inherit lib; inherit lib;
rootMountPoint = config.disko.rootMountPoint; inherit (config.disko) rootMountPoint;
}; };
cfg = config.disko; cfg = config.disko;
in { in
{
options.disko = { options.disko = {
devices = lib.mkOption { devices = lib.mkOption {
type = types.devices; type = types.devices;
default = {}; default = { };
description = "The devices to set up"; description = "The devices to set up";
}; };
rootMountPoint = lib.mkOption { rootMountPoint = lib.mkOption {
@ -27,7 +28,7 @@ in {
default = true; default = true;
}; };
}; };
config = lib.mkIf (cfg.devices.disk != {}) { config = lib.mkIf (cfg.devices.disk != { }) {
system.build.formatScript = pkgs.writers.writeBash "disko-create" '' system.build.formatScript = pkgs.writers.writeBash "disko-create" ''
export PATH=${lib.makeBinPath (types.diskoLib.packages cfg.devices pkgs)}:$PATH export PATH=${lib.makeBinPath (types.diskoLib.packages cfg.devices pkgs)}:$PATH
${types.diskoLib.create cfg.devices} ${types.diskoLib.create cfg.devices}

View file

@ -3,8 +3,8 @@
, pkgs ? (import <nixpkgs> { }) , pkgs ? (import <nixpkgs> { })
}@args: }@args:
let let
lib = pkgs.lib; inherit (pkgs) lib;
makeDiskoTest = (pkgs.callPackage ./lib.nix { inherit makeTest eval-config; }).makeDiskoTest; inherit ((pkgs.callPackage ./lib.nix { inherit makeTest eval-config; })) makeDiskoTest;
evalTest = name: configFile: let evalTest = name: configFile: let
disko-config = import configFile; disko-config = import configFile;
@ -20,7 +20,7 @@ let
(lib.attrNames (builtins.readDir ./.)) (lib.attrNames (builtins.readDir ./.))
); );
allTests = lib.genAttrs (allTestFilenames) (test: import (./. + "/${test}.nix") { inherit makeDiskoTest pkgs; }) // allTests = lib.genAttrs allTestFilenames (test: import (./. + "/${test}.nix") { inherit makeDiskoTest pkgs; }) //
evalTest "lvm-luks-example" ../example/config.nix // { evalTest "lvm-luks-example" ../example/config.nix // {
standalone = (pkgs.nixos [ ../example/stand-alone/configuration.nix ]).config.system.build.toplevel; standalone = (pkgs.nixos [ ../example/stand-alone/configuration.nix ]).config.system.build.toplevel;
}; };

View file

@ -17,7 +17,7 @@
, testBoot ? true # if we actually want to test booting or just create/mount , testBoot ? true # if we actually want to test booting or just create/mount
}: }:
let let
lib = pkgs.lib; inherit (pkgs) lib;
makeTest' = args: makeTest' = args:
makeTest args { makeTest args {
inherit pkgs; inherit pkgs;

View file

@ -48,7 +48,7 @@
subvolMounts = diskoLib.deepMergeMap (subvol: subvol._mount { inherit dev; parent = config.mountpoint; }) (lib.attrValues config.subvolumes); subvolMounts = diskoLib.deepMergeMap (subvol: subvol._mount { inherit dev; parent = config.mountpoint; }) (lib.attrValues config.subvolumes);
in in
{ {
fs = subvolMounts.fs // lib.optionalAttrs (!isNull config.mountpoint) { fs = subvolMounts.fs // lib.optionalAttrs (config.mountpoint != null) {
${config.mountpoint} = '' ${config.mountpoint} = ''
if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
mount ${dev} "${rootMountPoint}${config.mountpoint}" \ mount ${dev} "${rootMountPoint}${config.mountpoint}" \
@ -64,7 +64,7 @@
readOnly = true; readOnly = true;
default = dev: [ default = dev: [
(map (subvol: subvol._config dev config.mountpoint) (lib.attrValues config.subvolumes)) (map (subvol: subvol._config dev config.mountpoint) (lib.attrValues config.subvolumes))
(lib.optional (!isNull config.mountpoint) { (lib.optional (config.mountpoint != null) {
fileSystems.${config.mountpoint} = { fileSystems.${config.mountpoint} = {
device = dev; device = dev;
fsType = "btrfs"; fsType = "btrfs";

View file

@ -50,11 +50,11 @@
default = { dev, parent }: default = { dev, parent }:
let let
mountpoint = mountpoint =
if (!isNull config.mountpoint) then config.mountpoint if (config.mountpoint != null) then config.mountpoint
else if (isNull parent) then config.name else if (parent == null) then config.name
else null; else null;
in in
lib.optionalAttrs (!isNull mountpoint) { lib.optionalAttrs (mountpoint != null) {
fs.${mountpoint} = '' fs.${mountpoint} = ''
if ! findmnt ${dev} "${rootMountPoint}${mountpoint}" > /dev/null 2>&1; then if ! findmnt ${dev} "${rootMountPoint}${mountpoint}" > /dev/null 2>&1; then
mount ${dev} "${rootMountPoint}${mountpoint}" \ mount ${dev} "${rootMountPoint}${mountpoint}" \
@ -70,11 +70,11 @@
default = dev: parent: default = dev: parent:
let let
mountpoint = mountpoint =
if (!isNull config.mountpoint) then config.mountpoint if (config.mountpoint != null) then config.mountpoint
else if (isNull parent) then config.name else if (parent == null) then config.name
else null; else null;
in in
lib.optional (!isNull mountpoint) { lib.optional (mountpoint != null) {
fileSystems.${mountpoint} = { fileSystems.${mountpoint} = {
device = dev; device = dev;
fsType = "btrfs"; fsType = "btrfs";

View file

@ -11,8 +11,7 @@ rec {
name = "subType"; name = "subType";
description = "one of ${concatStringsSep "," (attrNames typeAttr)}"; description = "one of ${concatStringsSep "," (attrNames typeAttr)}";
check = x: if x ? type then typeAttr.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}"; check = x: if x ? type then typeAttr.${x.type}.check x else throw "No type option set in:\n${generators.toPretty {} x}";
merge = loc: defs: merge = loc: foldl' (res: def: typeAttr.${def.value.type}.merge loc [ def ]) { };
foldl' (res: def: typeAttr.${def.value.type}.merge loc [ def ]) { } defs;
nestedTypes = typeAttr; nestedTypes = typeAttr;
}; };
@ -38,8 +37,7 @@ rec {
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: listOfAttrs: deepMergeMap = f: foldr (attr: acc: (recursiveUpdate acc (f attr))) { };
foldr (attr: acc: (recursiveUpdate acc (f attr))) { } listOfAttrs;
/* get a device and an index to get the matching device name /* get a device and an index to get the matching device name
@ -119,7 +117,7 @@ rec {
maybeSTr "hello world" maybeSTr "hello world"
=> "hello world" => "hello world"
*/ */
maybeStr = x: optionalString (!isNull x) x; maybeStr = x: 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.
@ -147,7 +145,7 @@ rec {
default = ""; default = "";
}; };
mkSubType = module: lib.types.submodule ([ mkSubType = module: lib.types.submodule [
module module
{ {
@ -161,7 +159,7 @@ rec {
inherit diskoLib optionTypes subTypes rootMountPoint; inherit diskoLib optionTypes subTypes rootMountPoint;
}; };
} }
]); ];
mkCreateOption = { config, options, default }@attrs: mkCreateOption = { config, options, default }@attrs:
lib.mkOption { lib.mkOption {
@ -189,7 +187,7 @@ rec {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo diskoLib.jsonType; type = lib.types.functionTo diskoLib.jsonType;
default = args: attrs.default args; inherit (attrs) default;
description = "Mount script"; description = "Mount script";
}; };
@ -215,7 +213,7 @@ rec {
trap 'rm -rf "$disko_devices_dir"' EXIT trap 'rm -rf "$disko_devices_dir"' EXIT
mkdir -p "$disko_devices_dir" mkdir -p "$disko_devices_dir"
${concatMapStrings (dev: (attrByPath (dev ++ [ "_create" ]) ({}: {}) devices) {}) sortedDeviceList} ${concatMapStrings (dev: (attrByPath (dev ++ [ "_create" ]) (_: {}) devices) {}) sortedDeviceList}
''; '';
/* Takes a disko device specification and returns a string which mounts the disks /* Takes a disko device specification and returns a string which mounts the disks
@ -326,7 +324,7 @@ rec {
}; };
}; };
subTypes = lib.mapAttrs (_: module: diskoLib.mkSubType module) { subTypes = lib.mapAttrs (_: diskoLib.mkSubType) {
nodev = ./nodev.nix; nodev = ./nodev.nix;
btrfs = ./btrfs.nix; btrfs = ./btrfs.nix;
btrfs_subvol = ./btrfs_subvol.nix; btrfs_subvol = ./btrfs_subvol.nix;

View file

@ -22,30 +22,30 @@
readOnly = true; readOnly = true;
type = diskoLib.jsonType; type = diskoLib.jsonType;
default = default =
lib.optionalAttrs (!isNull config.content) (config.content._meta [ "disk" config.device ]); lib.optionalAttrs (config.content != null) (config.content._meta [ "disk" config.device ]);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = {}: config.content._create { dev = config.device; }; default = _: config.content._create { dev = config.device; };
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = {}: default = _:
lib.optionalAttrs (!isNull config.content) (config.content._mount { dev = config.device; }); lib.optionalAttrs (config.content != null) (config.content._mount { dev = config.device; });
}; };
_config = lib.mkOption { _config = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
default = default =
lib.optional (!isNull config.content) (config.content._config config.device); lib.optional (config.content != null) (config.content._config config.device);
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.jq ] ++ lib.optionals (!isNull config.content) (config.content._pkgs pkgs); default = pkgs: [ pkgs.jq ] ++ lib.optionals (config.content != null) (config.content._pkgs pkgs);
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -26,15 +26,15 @@
readOnly = true; readOnly = true;
type = lib.types.functionTo diskoLib.jsonType; type = lib.types.functionTo diskoLib.jsonType;
default = dev: default = dev:
lib.optionalAttrs (!isNull config.content) (config.content._meta dev); lib.optionalAttrs (config.content != null) (config.content._meta dev);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = { dev }: '' default = { dev }: ''
cryptsetup -q luksFormat ${dev} ${diskoLib.maybeStr config.keyFile} ${toString config.extraArgs} cryptsetup -q luksFormat ${dev} ${diskoLib.maybeStr config.keyFile} ${toString config.extraArgs}
cryptsetup luksOpen ${dev} ${config.name} ${lib.optionalString (!isNull config.keyFile) "--key-file ${config.keyFile}"} cryptsetup luksOpen ${dev} ${config.name} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
${lib.optionalString (!isNull config.content) (config.content._create {dev = "/dev/mapper/${config.name}";})} ${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/mapper/${config.name}";})}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
@ -46,10 +46,10 @@
{ {
dev = '' dev = ''
cryptsetup status ${config.name} >/dev/null 2>/dev/null || cryptsetup status ${config.name} >/dev/null 2>/dev/null ||
cryptsetup luksOpen ${dev} ${config.name} ${lib.optionalString (!isNull config.keyFile) "--key-file ${config.keyFile}"} cryptsetup luksOpen ${dev} ${config.name} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
${lib.optionalString (!isNull config.content) contentMount.dev or ""} ${lib.optionalString (config.content != null) contentMount.dev or ""}
''; '';
fs = lib.optionalAttrs (!isNull config.content) contentMount.fs or { }; fs = lib.optionalAttrs (config.content != null) contentMount.fs or { };
}; };
}; };
_config = lib.mkOption { _config = lib.mkOption {
@ -59,14 +59,14 @@
[ [
# TODO do we need this always in initrd and only there? # TODO do we need this always in initrd and only there?
{ boot.initrd.luks.devices.${config.name}.device = dev; } { boot.initrd.luks.devices.${config.name}.device = dev; }
] ++ (lib.optional (!isNull config.content) (config.content._config "/dev/mapper/${config.name}")); ] ++ (lib.optional (config.content != null) (config.content._config "/dev/mapper/${config.name}"));
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (!isNull config.content) (config.content._pkgs pkgs)); default = pkgs: [ pkgs.cryptsetup ] ++ (lib.optionals (config.content != null) (config.content._pkgs pkgs));
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -32,7 +32,7 @@
readOnly = true; readOnly = true;
type = lib.types.functionTo diskoLib.jsonType; type = lib.types.functionTo diskoLib.jsonType;
default = dev: default = dev:
lib.optionalAttrs (!isNull config.content) (config.content._meta dev); lib.optionalAttrs (config.content != null) (config.content._meta dev);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
@ -42,24 +42,24 @@
--yes \ --yes \
${if lib.hasInfix "%" config.size then "-l" else "-L"} ${config.size} \ ${if lib.hasInfix "%" config.size then "-l" else "-L"} ${config.size} \
-n ${config.name} \ -n ${config.name} \
${lib.optionalString (!isNull config.lvm_type) "--type=${config.lvm_type}"} \ ${lib.optionalString (config.lvm_type != null) "--type=${config.lvm_type}"} \
${config.extraArgs} \ ${config.extraArgs} \
${vg} ${vg}
${lib.optionalString (!isNull config.content) (config.content._create {dev = "/dev/${vg}/${config.name}";})} ${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/${vg}/${config.name}";})}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = { vg }: default = { vg }:
lib.optionalAttrs (!isNull config.content) (config.content._mount { dev = "/dev/${vg}/${config.name}"; }); lib.optionalAttrs (config.content != null) (config.content._mount { dev = "/dev/${vg}/${config.name}"; });
}; };
_config = lib.mkOption { _config = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
default = vg: default = vg:
[ [
(lib.optional (!isNull config.content) (config.content._config "/dev/${vg}/${config.name}")) (lib.optional (config.content != null) (config.content._config "/dev/${vg}/${config.name}"))
(lib.optional (!isNull config.lvm_type) { (lib.optional (config.lvm_type != null) {
boot.initrd.kernelModules = [ "dm-${config.lvm_type}" ]; boot.initrd.kernelModules = [ "dm-${config.lvm_type}" ];
}) })
]; ];
@ -69,7 +69,7 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: lib.optionals (!isNull config.content) (config.content._pkgs pkgs); default = pkgs: lib.optionals (config.content != null) (config.content._pkgs pkgs);
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -26,14 +26,14 @@
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = {}: '' default = _: ''
vgcreate ${config.name} $(tr '\n' ' ' < $disko_devices_dir/lvm_${config.name}) vgcreate ${config.name} $(tr '\n' ' ' < $disko_devices_dir/lvm_${config.name})
${lib.concatMapStrings (lv: lv._create {vg = config.name; }) (lib.attrValues config.lvs)} ${lib.concatMapStrings (lv: lv._create {vg = config.name; }) (lib.attrValues config.lvs)}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = {}: default = _:
let let
lvMounts = diskoLib.deepMergeMap (lv: lv._mount { vg = config.name; }) (lib.attrValues config.lvs); lvMounts = diskoLib.deepMergeMap (lv: lv._mount { vg = config.name; }) (lib.attrValues config.lvs);
in in
@ -42,7 +42,7 @@
vgchange -a y vgchange -a y
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues lvMounts)} ${lib.concatMapStrings (x: x.dev or "") (lib.attrValues lvMounts)}
''; '';
fs = lvMounts.fs; inherit (lvMounts) fs;
}; };
}; };
_config = lib.mkOption { _config = lib.mkOption {

View file

@ -28,12 +28,12 @@
readOnly = true; readOnly = true;
type = diskoLib.jsonType; type = diskoLib.jsonType;
default = default =
lib.optionalAttrs (!isNull config.content) (config.content._meta [ "mdadm" config.name ]); lib.optionalAttrs (config.content != null) (config.content._meta [ "mdadm" config.name ]);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = {}: '' default = _: ''
echo 'y' | mdadm --create /dev/md/${config.name} \ echo 'y' | mdadm --create /dev/md/${config.name} \
--level=${toString config.level} \ --level=${toString config.level} \
--raid-devices=$(wc -l $disko_devices_dir/raid_${config.name} | cut -f 1 -d " ") \ --raid-devices=$(wc -l $disko_devices_dir/raid_${config.name} | cut -f 1 -d " ") \
@ -42,27 +42,27 @@
--homehost=any \ --homehost=any \
$(tr '\n' ' ' < $disko_devices_dir/raid_${config.name}) $(tr '\n' ' ' < $disko_devices_dir/raid_${config.name})
udevadm trigger --subsystem-match=block; udevadm settle udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString (!isNull config.content) (config.content._create {dev = "/dev/md/${config.name}";})} ${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/md/${config.name}";})}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = {}: default = _:
lib.optionalAttrs (!isNull config.content) (config.content._mount { dev = "/dev/md/${config.name}"; }); lib.optionalAttrs (config.content != null) (config.content._mount { dev = "/dev/md/${config.name}"; });
# TODO we probably need to assemble the mdadm somehow # TODO we probably need to assemble the mdadm somehow
}; };
_config = lib.mkOption { _config = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
default = default =
lib.optional (!isNull config.content) (config.content._config "/dev/md/${config.name}"); lib.optional (config.content != null) (config.content._config "/dev/md/${config.name}");
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: (lib.optionals (!isNull config.content) (config.content._pkgs pkgs)); default = pkgs: (lib.optionals (config.content != null) (config.content._pkgs pkgs));
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -35,11 +35,11 @@
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = {}: ""; default = _: "";
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = {}: { default = _: {
fs.${config.mountpoint} = '' fs.${config.mountpoint} = ''
if ! findmnt ${config.fsType} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then if ! findmnt ${config.fsType} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
mount -t ${config.fsType} ${config.device} "${rootMountPoint}${config.mountpoint}" \ mount -t ${config.fsType} ${config.device} "${rootMountPoint}${config.mountpoint}" \
@ -54,8 +54,8 @@
readOnly = true; readOnly = true;
default = [{ default = [{
fileSystems.${config.mountpoint} = { fileSystems.${config.mountpoint} = {
device = config.device; inherit (config) device;
fsType = config.fsType; inherit (config) fsType;
options = config.mountOptions; options = config.mountOptions;
}; };
}]; }];

View file

@ -52,7 +52,7 @@
readOnly = true; readOnly = true;
type = lib.types.functionTo diskoLib.jsonType; type = lib.types.functionTo diskoLib.jsonType;
default = dev: default = dev:
lib.optionalAttrs (!isNull config.content) (config.content._meta dev); lib.optionalAttrs (config.content != null) (config.content._meta dev);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
@ -66,7 +66,7 @@
''} ''}
# ensure /dev/disk/by-path/..-partN exists before continuing # ensure /dev/disk/by-path/..-partN exists before continuing
udevadm trigger --subsystem-match=block; udevadm settle udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString (config.bootable) '' ${lib.optionalString config.bootable ''
parted -s ${dev} -- set ${toString config.index} boot on parted -s ${dev} -- set ${toString config.index} boot on
''} ''}
${lib.concatMapStringsSep "" (flag: '' ${lib.concatMapStringsSep "" (flag: ''
@ -74,26 +74,26 @@
'') config.flags} '') config.flags}
# ensure further operations can detect new partitions # ensure further operations can detect new partitions
udevadm trigger --subsystem-match=block; udevadm settle udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString (!isNull config.content) (config.content._create {dev = (diskoLib.deviceNumbering dev config.index);})} ${lib.optionalString (config.content != null) (config.content._create {dev = diskoLib.deviceNumbering dev config.index;})}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = { dev }: default = { dev }:
lib.optionalAttrs (!isNull config.content) (config.content._mount { dev = (diskoLib.deviceNumbering dev config.index); }); lib.optionalAttrs (config.content != null) (config.content._mount { dev = diskoLib.deviceNumbering dev config.index; });
}; };
_config = lib.mkOption { _config = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
default = dev: default = dev:
lib.optional (!isNull config.content) (config.content._config (diskoLib.deviceNumbering dev config.index)); lib.optional (config.content != null) (config.content._config (diskoLib.deviceNumbering dev config.index));
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: lib.optionals (!isNull config.content) (config.content._pkgs pkgs); default = pkgs: lib.optionals (config.content != null) (config.content._pkgs pkgs);
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -40,7 +40,7 @@
default = dev: [{ default = dev: [{
swapDevices = [{ swapDevices = [{
device = dev; device = dev;
randomEncryption = config.randomEncryption; inherit (config) randomEncryption;
}]; }];
}]; }];
description = "NixOS configuration"; description = "NixOS configuration";

View file

@ -47,7 +47,7 @@
readOnly = true; readOnly = true;
type = lib.types.functionTo diskoLib.jsonType; type = lib.types.functionTo diskoLib.jsonType;
default = dev: default = dev:
lib.optionalAttrs (!isNull config.content) (config.content._meta dev); lib.optionalAttrs (config.content != null) (config.content._meta dev);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
@ -58,14 +58,14 @@
${lib.optionalString (config.zfs_type == "volume") "-V ${config.size}"} ${lib.optionalString (config.zfs_type == "volume") "-V ${config.size}"}
${lib.optionalString (config.zfs_type == "volume") '' ${lib.optionalString (config.zfs_type == "volume") ''
udevadm trigger --subsystem-match=block; udevadm settle udevadm trigger --subsystem-match=block; udevadm settle
${lib.optionalString (!isNull config.content) (config.content._create {dev = "/dev/zvol/${zpool}/${config.name}";})} ${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/zvol/${zpool}/${config.name}";})}
''} ''}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = { zpool }: default = { zpool }:
lib.optionalAttrs (config.zfs_type == "volume" && !isNull config.content) (config.content._mount { dev = "/dev/zvol/${zpool}/${config.name}"; }) // lib.optionalAttrs (config.zfs_type == "volume" && config.content != null) (config.content._mount { dev = "/dev/zvol/${zpool}/${config.name}"; }) //
lib.optionalAttrs (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") { lib.optionalAttrs (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") {
fs.${config.mountpoint} = '' fs.${config.mountpoint} = ''
if ! findmnt ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then if ! findmnt ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
@ -82,7 +82,7 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
default = zpool: default = zpool:
(lib.optional (config.zfs_type == "volume" && !isNull config.content) (config.content._config "/dev/zvol/${zpool}/${config.name}")) ++ (lib.optional (config.zfs_type == "volume" && config.content != null) (config.content._config "/dev/zvol/${zpool}/${config.name}")) ++
(lib.optional (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") { (lib.optional (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") {
fileSystems.${config.mountpoint} = { fileSystems.${config.mountpoint} = {
device = "${zpool}/${config.name}"; device = "${zpool}/${config.name}";
@ -96,7 +96,7 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: [ pkgs.util-linux ] ++ lib.optionals (!isNull config.content) (config.content._pkgs pkgs); default = pkgs: [ pkgs.util-linux ] ++ lib.optionals (config.content != null) (config.content._pkgs pkgs);
description = "Packages"; description = "Packages";
}; };
}; };

View file

@ -51,7 +51,7 @@
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
inherit config options; inherit config options;
default = {}: '' default = _: ''
zpool create ${config.name} \ zpool create ${config.name} \
${config.mode} \ ${config.mode} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \ ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
@ -62,7 +62,7 @@
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = {}: default = _:
let let
datasetMounts = diskoLib.deepMergeMap (dataset: dataset._mount { zpool = config.name; }) (lib.attrValues config.datasets); datasetMounts = diskoLib.deepMergeMap (dataset: dataset._mount { zpool = config.name; }) (lib.attrValues config.datasets);
in in
@ -71,7 +71,7 @@
zpool list '${config.name}' >/dev/null 2>/dev/null || zpool import '${config.name}' zpool list '${config.name}' >/dev/null 2>/dev/null || zpool import '${config.name}'
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)} ${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)}
''; '';
fs = datasetMounts.fs // lib.optionalAttrs (!isNull config.mountpoint) { fs = datasetMounts.fs // lib.optionalAttrs (config.mountpoint != null) {
${config.mountpoint} = '' ${config.mountpoint} = ''
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
mount ${config.name} "${rootMountPoint}${config.mountpoint}" \ mount ${config.name} "${rootMountPoint}${config.mountpoint}" \
@ -89,7 +89,7 @@
readOnly = true; readOnly = true;
default = [ default = [
(map (dataset: dataset._config config.name) (lib.attrValues config.datasets)) (map (dataset: dataset._config config.name) (lib.attrValues config.datasets))
(lib.optional (!isNull config.mountpoint) { (lib.optional (config.mountpoint != null) {
fileSystems.${config.mountpoint} = { fileSystems.${config.mountpoint} = {
device = config.name; device = config.name;
fsType = "zfs"; fsType = "zfs";