mirror of
https://github.com/nix-community/disko.git
synced 2026-12-02 22:13:28 +01:00
lib: Fix jsonTypes evaluation
This will be very useful for generating documentation.
Backported from a5c646bd93 for #789
This commit is contained in:
1 parent
5fd852c415
commit
9f97bd0995
5 files changed
+90
-22
No files matched your search
@@ -19,13 +19,15 @@
|
|||||||
|
|
||||||
versionInfo = import ./version.nix;
|
versionInfo = import ./version.nix;
|
||||||
version = versionInfo.version + (lib.optionalString (!versionInfo.released) "-dirty");
|
version = versionInfo.version + (lib.optionalString (!versionInfo.released) "-dirty");
|
||||||
|
|
||||||
|
diskoLib = import ./lib {
|
||||||
|
inherit (nixpkgs) lib;
|
||||||
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
nixosModules.default = self.nixosModules.disko; # convention
|
nixosModules.default = self.nixosModules.disko; # convention
|
||||||
nixosModules.disko.imports = [ ./module.nix ];
|
nixosModules.disko.imports = [ ./module.nix ];
|
||||||
lib = import ./lib {
|
lib = diskoLib;
|
||||||
inherit (nixpkgs) lib;
|
|
||||||
};
|
|
||||||
packages = forAllSystems (system:
|
packages = forAllSystems (system:
|
||||||
let
|
let
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
@@ -64,11 +66,13 @@
|
|||||||
shellcheck disk-deactivate/disk-deactivate disko
|
shellcheck disk-deactivate/disk-deactivate disko
|
||||||
touch $out
|
touch $out
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
jsonTypes = pkgs.writeTextFile { name = "jsonTypes"; text = (builtins.toJSON diskoLib.jsonTypes); };
|
||||||
in
|
in
|
||||||
# FIXME: aarch64-linux seems to hang on boot
|
# FIXME: aarch64-linux seems to hang on boot
|
||||||
lib.optionalAttrs pkgs.stdenv.hostPlatform.isx86_64 (nixosTests // { inherit disko-install; }) //
|
lib.optionalAttrs pkgs.stdenv.hostPlatform.isx86_64 (nixosTests // { inherit disko-install; }) //
|
||||||
pkgs.lib.optionalAttrs (!pkgs.stdenv.buildPlatform.isRiscV64 && !pkgs.stdenv.hostPlatform.isx86_32) {
|
pkgs.lib.optionalAttrs (!pkgs.stdenv.buildPlatform.isRiscV64 && !pkgs.stdenv.hostPlatform.isx86_32) {
|
||||||
inherit shellcheck;
|
inherit shellcheck jsonTypes;
|
||||||
inherit (self.packages.${system}) disko-doc;
|
inherit (self.packages.${system}) disko-doc;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+60
-18
@@ -23,9 +23,10 @@ let
|
|||||||
};
|
};
|
||||||
|
|
||||||
# option for valid contents of partitions (basically like devices, but without tables)
|
# option for valid contents of partitions (basically like devices, but without tables)
|
||||||
|
_partitionTypes = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; };
|
||||||
partitionType = extraArgs: lib.mkOption {
|
partitionType = extraArgs: lib.mkOption {
|
||||||
type = lib.types.nullOr (diskoLib.subType {
|
type = lib.types.nullOr (diskoLib.subType {
|
||||||
types = { inherit (diskoLib.types) btrfs filesystem zfs mdraid luks lvm_pv swap; };
|
types = diskoLib._partitionTypes;
|
||||||
inherit extraArgs;
|
inherit extraArgs;
|
||||||
});
|
});
|
||||||
default = null;
|
default = null;
|
||||||
@@ -33,9 +34,10 @@ let
|
|||||||
};
|
};
|
||||||
|
|
||||||
# option for valid contents of devices
|
# option for valid contents of devices
|
||||||
|
_deviceTypes = { inherit (diskoLib.types) table gpt btrfs filesystem zfs mdraid luks lvm_pv swap; };
|
||||||
deviceType = extraArgs: lib.mkOption {
|
deviceType = extraArgs: lib.mkOption {
|
||||||
type = lib.types.nullOr (diskoLib.subType {
|
type = lib.types.nullOr (diskoLib.subType {
|
||||||
types = { inherit (diskoLib.types) table gpt btrfs filesystem zfs mdraid luks lvm_pv swap; };
|
types = diskoLib._deviceTypes;
|
||||||
inherit extraArgs;
|
inherit extraArgs;
|
||||||
});
|
});
|
||||||
default = null;
|
default = null;
|
||||||
@@ -699,11 +701,22 @@ let
|
|||||||
typesSerializerLib = {
|
typesSerializerLib = {
|
||||||
rootMountPoint = "";
|
rootMountPoint = "";
|
||||||
options = null;
|
options = null;
|
||||||
config._module.args.name = "self.name";
|
config = {
|
||||||
lib = {
|
_module = {
|
||||||
|
args.name = "<self.name>";
|
||||||
|
args._parent.name = "<parent.name>";
|
||||||
|
args._parent.type = "<parent.type>";
|
||||||
|
};
|
||||||
|
name = "<config.name>";
|
||||||
|
};
|
||||||
|
parent = { };
|
||||||
|
device = "/dev/<device>";
|
||||||
|
# Spoof part of nixpkgs/lib to analyze the types
|
||||||
|
lib = lib // {
|
||||||
mkOption = option: {
|
mkOption = option: {
|
||||||
inherit (option) type description;
|
inherit (option) type;
|
||||||
default = option.default or null;
|
description = option.description or null;
|
||||||
|
default = option.defaultText or option.default or null;
|
||||||
};
|
};
|
||||||
types = {
|
types = {
|
||||||
attrsOf = subType: {
|
attrsOf = subType: {
|
||||||
@@ -718,32 +731,61 @@ let
|
|||||||
type = "nullOr";
|
type = "nullOr";
|
||||||
inherit subType;
|
inherit subType;
|
||||||
};
|
};
|
||||||
|
oneOf = types: {
|
||||||
|
type = "oneOf";
|
||||||
|
inherit types;
|
||||||
|
};
|
||||||
|
either = t1: t2: {
|
||||||
|
type = "oneOf";
|
||||||
|
types = [ t1 t2 ];
|
||||||
|
};
|
||||||
enum = choices: {
|
enum = choices: {
|
||||||
type = "enum";
|
type = "enum";
|
||||||
inherit choices;
|
inherit choices;
|
||||||
};
|
};
|
||||||
|
anything = "anything";
|
||||||
|
nonEmptyStr = "str";
|
||||||
|
strMatching = _: "str";
|
||||||
str = "str";
|
str = "str";
|
||||||
bool = "bool";
|
bool = "bool";
|
||||||
int = "int";
|
int = "int";
|
||||||
submodule = x: x { inherit (diskoLib.typesSerializerLib) lib config options; };
|
submodule = x: x {
|
||||||
|
inherit (diskoLib.typesSerializerLib) lib config options;
|
||||||
|
name = "<self.name>";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
diskoLib = {
|
diskoLib = {
|
||||||
optionTypes.absolute-pathname = "absolute-pathname";
|
optionTypes.absolute-pathname = "absolute-pathname";
|
||||||
deviceType = "devicetype";
|
# Spoof these types to avoid infinite recursion
|
||||||
partitionType = "partitiontype";
|
deviceType = _: "<deviceType>";
|
||||||
subType = types: "onOf ${toString (lib.attrNames types)}";
|
partitionType = _: "<partitionType>";
|
||||||
|
subType = { types, ... }: {
|
||||||
|
type = "oneOf";
|
||||||
|
types = lib.attrNames types;
|
||||||
|
};
|
||||||
|
mkCreateOption = option: "_create";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
jsonTypes = lib.listToAttrs (
|
jsonTypes = lib.listToAttrs
|
||||||
map
|
(
|
||||||
(file: lib.nameValuePair
|
map
|
||||||
(lib.removeSuffix ".nix" file)
|
(file: lib.nameValuePair
|
||||||
(diskoLib.serializeType (import ./types/${file} diskoLib.typesSerializerLib))
|
(lib.removeSuffix ".nix" file)
|
||||||
)
|
(diskoLib.serializeType (import ./types/${file} diskoLib.typesSerializerLib))
|
||||||
(lib.attrNames (builtins.readDir ./types))
|
)
|
||||||
);
|
(lib.filter (name: lib.hasSuffix ".nix" name) (lib.attrNames (builtins.readDir ./types)))
|
||||||
|
) // {
|
||||||
|
partitionType = {
|
||||||
|
type = "oneOf";
|
||||||
|
types = lib.attrNames diskoLib._partitionTypes;
|
||||||
|
};
|
||||||
|
deviceType = {
|
||||||
|
type = "oneOf";
|
||||||
|
types = lib.attrNames diskoLib._deviceTypes;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
} // outputs;
|
} // outputs;
|
||||||
in
|
in
|
||||||
|
|||||||
@@ -42,6 +42,13 @@ in
|
|||||||
"/dev/disk/by-id/md-name-any:${config._parent.name}-part${toString partition.config._index}"
|
"/dev/disk/by-id/md-name-any:${config._parent.name}-part${toString partition.config._index}"
|
||||||
else
|
else
|
||||||
"/dev/disk/by-partlabel/${diskoLib.hexEscapeUdevSymlink partition.config.label}";
|
"/dev/disk/by-partlabel/${diskoLib.hexEscapeUdevSymlink partition.config.label}";
|
||||||
|
defaultText = ''
|
||||||
|
if the parent is an mdadm device:
|
||||||
|
/dev/disk/by-id/md-name-any:''${config._parent.name}-part''${toString partition.config._index}
|
||||||
|
|
||||||
|
otherwise:
|
||||||
|
/dev/disk/by-partlabel/''${diskoLib.hexEscapeUdevSymlink partition.config.label}
|
||||||
|
'';
|
||||||
description = "Device to use for the partition";
|
description = "Device to use for the partition";
|
||||||
};
|
};
|
||||||
priority = lib.mkOption {
|
priority = lib.mkOption {
|
||||||
@@ -80,6 +87,11 @@ in
|
|||||||
builtins.substring 0 limit (builtins.hashString "sha256" label)
|
builtins.substring 0 limit (builtins.hashString "sha256" label)
|
||||||
else
|
else
|
||||||
label;
|
label;
|
||||||
|
defaultText = ''
|
||||||
|
''${config._parent.type}-''${config._parent.name}-''${partition.config.name}
|
||||||
|
|
||||||
|
or a truncated hash of the above if it is longer than 36 characters
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
size = lib.mkOption {
|
size = lib.mkOption {
|
||||||
type = lib.types.either (lib.types.enum [ "100%" ]) (lib.types.strMatching "[0-9]+[KMGTP]?");
|
type = lib.types.either (lib.types.enum [ "100%" ]) (lib.types.strMatching "[0-9]+[KMGTP]?");
|
||||||
@@ -93,6 +105,7 @@ in
|
|||||||
alignment = lib.mkOption {
|
alignment = lib.mkOption {
|
||||||
type = lib.types.int;
|
type = lib.types.int;
|
||||||
default = if (builtins.substring (builtins.stringLength partition.config.start - 1) 1 partition.config.start == "s" || (builtins.substring (builtins.stringLength partition.config.end - 1) 1 partition.config.end == "s")) then 1 else 0;
|
default = if (builtins.substring (builtins.stringLength partition.config.start - 1) 1 partition.config.start == "s" || (builtins.substring (builtins.stringLength partition.config.end - 1) 1 partition.config.end == "s")) then 1 else 0;
|
||||||
|
defaultText = "1 if the unit of start or end is sectors, 0 otherwise";
|
||||||
description = "Alignment of the partition, if sectors are used as start or end it can be aligned to 1";
|
description = "Alignment of the partition, if sectors are used as start or end it can be aligned to 1";
|
||||||
};
|
};
|
||||||
start = lib.mkOption {
|
start = lib.mkOption {
|
||||||
@@ -103,6 +116,9 @@ in
|
|||||||
end = lib.mkOption {
|
end = lib.mkOption {
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
default = if partition.config.size == "100%" then "-0" else "+${partition.config.size}";
|
default = if partition.config.size == "100%" then "-0" else "+${partition.config.size}";
|
||||||
|
defaultText = ''
|
||||||
|
if partition.config.size == "100%" then "-0" else "+''${partition.config.size}";
|
||||||
|
'';
|
||||||
description = ''
|
description = ''
|
||||||
End of the partition, in sgdisk format.
|
End of the partition, in sgdisk format.
|
||||||
Use + for relative sizes from the partitions start
|
Use + for relative sizes from the partitions start
|
||||||
@@ -142,8 +158,10 @@ in
|
|||||||
description = "Entry to add to the Hybrid MBR table";
|
description = "Entry to add to the Hybrid MBR table";
|
||||||
};
|
};
|
||||||
_index = lib.mkOption {
|
_index = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
internal = true;
|
internal = true;
|
||||||
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
|
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
|
||||||
|
defaultText = null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -59,9 +59,11 @@ in
|
|||||||
askPassword = lib.mkOption {
|
askPassword = lib.mkOption {
|
||||||
type = lib.types.bool;
|
type = lib.types.bool;
|
||||||
default = config.keyFile == null && config.passwordFile == null && (! config.settings ? "keyFile");
|
default = config.keyFile == null && config.passwordFile == null && (! config.settings ? "keyFile");
|
||||||
|
defaultText = "true if neither keyFile nor passwordFile are set";
|
||||||
description = "Whether to ask for a password for initial encryption";
|
description = "Whether to ask for a password for initial encryption";
|
||||||
};
|
};
|
||||||
settings = lib.mkOption {
|
settings = lib.mkOption {
|
||||||
|
type = lib.types.attrsOf lib.types.anything;
|
||||||
default = { };
|
default = { };
|
||||||
description = "LUKS settings (as defined in configuration.nix in boot.initrd.luks.devices.<name>)";
|
description = "LUKS settings (as defined in configuration.nix in boot.initrd.luks.devices.<name>)";
|
||||||
example = ''{
|
example = ''{
|
||||||
|
|||||||
@@ -63,8 +63,10 @@
|
|||||||
};
|
};
|
||||||
content = diskoLib.partitionType { parent = config; device = diskoLib.deviceNumbering config.device partition.config._index; };
|
content = diskoLib.partitionType { parent = config; device = diskoLib.deviceNumbering config.device partition.config._index; };
|
||||||
_index = lib.mkOption {
|
_index = lib.mkOption {
|
||||||
|
type = lib.types.int;
|
||||||
internal = true;
|
internal = true;
|
||||||
default = lib.toInt (lib.head (builtins.match ".*entry ([[:digit:]]+)]" name));
|
default = lib.toInt (lib.head (builtins.match ".*entry ([[:digit:]]+)]" name));
|
||||||
|
defaultText = null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in new issue
Block a user