mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 21:13:28 +01:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21539593db | ||
|
|
6f579bd98a | ||
|
|
196c746f7e | ||
|
|
47081e80ea | ||
|
|
f2618b5490 | ||
|
|
684adb7e64 | ||
|
|
f34c39ec45 | ||
|
|
62581f38f5 | ||
|
|
94806e4506 | ||
|
|
3d67130ff3 |
No files matched your search
@@ -17,6 +17,7 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit eval;
|
||||
lib = lib.warn "the .lib.lib output is deprecated" diskoLib;
|
||||
|
||||
# legacy alias
|
||||
|
||||
Executable
+323
@@ -0,0 +1,323 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
use lib [eval-disko-file eval-flake exit-on-error print-info print-help]
|
||||
use disko2
|
||||
|
||||
use std assert
|
||||
|
||||
alias disko = disko2 run
|
||||
|
||||
assert equal ("example/simple-efi.nix" | path expand | eval-disko-file) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
"main": {
|
||||
"content": {
|
||||
"device": "/dev/disk/by-id/some-disk-id",
|
||||
"efiGptPartitionFirst": true,
|
||||
"partitions": {
|
||||
"ESP": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"extraArgs": [],
|
||||
"format": "vfat",
|
||||
"mountOptions": [
|
||||
"umask=0077"
|
||||
],
|
||||
"mountpoint": "/boot",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"end": "+500M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-ESP",
|
||||
"name": "ESP",
|
||||
"priority": 1000,
|
||||
"size": "500M",
|
||||
"start": "0",
|
||||
"type": "EF00"
|
||||
},
|
||||
"root": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"extraArgs": [],
|
||||
"format": "ext4",
|
||||
"mountOptions": [
|
||||
"defaults"
|
||||
],
|
||||
"mountpoint": "/",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"end": "-0",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-root",
|
||||
"name": "root",
|
||||
"priority": 9001,
|
||||
"size": "100%",
|
||||
"start": "0",
|
||||
"type": "8300"
|
||||
}
|
||||
},
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "gpt"
|
||||
},
|
||||
"device": "/dev/disk/by-id/some-disk-id",
|
||||
"imageName": "main",
|
||||
"imageSize": "2G",
|
||||
"name": "main",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "disk"
|
||||
}
|
||||
},
|
||||
"lvm_vg": {},
|
||||
"mdadm": {},
|
||||
"nodev": {},
|
||||
"zpool": {}
|
||||
}
|
||||
}
|
||||
|
||||
assert equal ("example/with-lib.nix" | path expand | eval-disko-file) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
"/dev/vdb": {
|
||||
"content": {
|
||||
"device": "/dev/vdb",
|
||||
"efiGptPartitionFirst": true,
|
||||
"partitions": {
|
||||
"boot": {
|
||||
"alignment": 0,
|
||||
"content": null,
|
||||
"device": "/dev/disk/by-partlabel/disk-_dev_vdb-boot",
|
||||
"end": "+1M",
|
||||
"hybrid": null,
|
||||
"label": "disk-_dev_vdb-boot",
|
||||
"name": "boot",
|
||||
"priority": 100,
|
||||
"size": "1M",
|
||||
"start": "0",
|
||||
"type": "EF02"
|
||||
},
|
||||
"root": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-_dev_vdb-root",
|
||||
"extraArgs": [],
|
||||
"format": "ext4",
|
||||
"mountOptions": [
|
||||
"defaults"
|
||||
],
|
||||
"mountpoint": "/",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-_dev_vdb-root",
|
||||
"end": "-0",
|
||||
"hybrid": null,
|
||||
"label": "disk-_dev_vdb-root",
|
||||
"name": "root",
|
||||
"priority": 9001,
|
||||
"size": "100%",
|
||||
"start": "0",
|
||||
"type": "8300"
|
||||
}
|
||||
},
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "gpt"
|
||||
},
|
||||
"device": "/dev/vdb",
|
||||
"imageName": "_dev_vdb",
|
||||
"imageSize": "2G",
|
||||
"name": "_dev_vdb",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "disk"
|
||||
}
|
||||
},
|
||||
"lvm_vg": {},
|
||||
"mdadm": {},
|
||||
"nodev": {},
|
||||
"zpool": {}
|
||||
}
|
||||
}
|
||||
|
||||
assert equal (".#testmachine" | eval-flake) {
|
||||
success: true,
|
||||
value: {
|
||||
"disk": {
|
||||
"main": {
|
||||
"content": {
|
||||
"device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345",
|
||||
"efiGptPartitionFirst": true,
|
||||
"partitions": {
|
||||
"ESP": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"extraArgs": [],
|
||||
"format": "vfat",
|
||||
"mountOptions": [
|
||||
"umask=0077"
|
||||
],
|
||||
"mountpoint": "/boot",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"end": "+512M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-ESP",
|
||||
"name": "ESP",
|
||||
"priority": 1000,
|
||||
"size": "512M",
|
||||
"start": "0",
|
||||
"type": "EF00"
|
||||
},
|
||||
"boot": {
|
||||
"alignment": 0,
|
||||
"content": null,
|
||||
"device": "/dev/disk/by-partlabel/disk-main-boot",
|
||||
"end": "+1M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-boot",
|
||||
"name": "boot",
|
||||
"priority": 100,
|
||||
"size": "1M",
|
||||
"start": "0",
|
||||
"type": "EF02"
|
||||
},
|
||||
"root": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"extraArgs": [],
|
||||
"format": "ext4",
|
||||
"mountOptions": [
|
||||
"defaults"
|
||||
],
|
||||
"mountpoint": "/",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"end": "-0",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-root",
|
||||
"name": "root",
|
||||
"priority": 9001,
|
||||
"size": "100%",
|
||||
"start": "0",
|
||||
"type": "8300"
|
||||
}
|
||||
},
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "gpt"
|
||||
},
|
||||
"device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345",
|
||||
"imageName": "main",
|
||||
"imageSize": "2G",
|
||||
"name": "main",
|
||||
"postCreateHook": "",
|
||||
"postMountHook": "",
|
||||
"preCreateHook": "",
|
||||
"preMountHook": "",
|
||||
"type": "disk"
|
||||
}
|
||||
},
|
||||
"lvm_vg": {},
|
||||
"mdadm": {},
|
||||
"nodev": {},
|
||||
"zpool": {}
|
||||
}
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "mount", flake: "."}) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_FLAKE_URI_NO_ATTRIBUTE,
|
||||
details: {
|
||||
flakeUri: "."
|
||||
}
|
||||
}
|
||||
],
|
||||
context: "evaluate flake"
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "qwert" }) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_INVALID_MODE,
|
||||
details: {
|
||||
mode: "qwert",
|
||||
valid_modes: [
|
||||
"destroy",
|
||||
"format",
|
||||
"mount",
|
||||
"format,mount",
|
||||
"destroy,format,mount"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
context: "validate mode"
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "mount" }) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_MISSING_ARGUMENTS
|
||||
}
|
||||
],
|
||||
context: "validate arguments"
|
||||
}
|
||||
|
||||
assert equal (disko { mode: "mount", flake: "irrelevant", disko_file: "whatever" }) {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: ERR_TOO_MANY_ARGUMENTS
|
||||
}
|
||||
],
|
||||
context: "validate arguments"
|
||||
}
|
||||
|
||||
def main [] {
|
||||
echo "All tests passed"
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
use lib [eval-disko-file eval-flake exit-on-error print-info print-help generate-config]
|
||||
|
||||
def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount", "generate"] }
|
||||
|
||||
def with-context [context: string] {
|
||||
$in | insert context $context
|
||||
}
|
||||
|
||||
export def run [
|
||||
args: record
|
||||
]: [
|
||||
nothing -> record<success: bool, context: string, value: any>
|
||||
nothing -> record<success: bool, context: string, messages: list<record<code: string>>>
|
||||
nothing -> record<success: bool, context: string, messages: list<record<code: string, details: record<any>>>>
|
||||
] {
|
||||
|
||||
if not ($args.mode in (modes)) {
|
||||
return {
|
||||
success: false,
|
||||
messages: [ { code: ERR_INVALID_MODE, details: { mode: $args.mode, valid_modes: (modes) } } ]
|
||||
context: "validate mode"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
match $args.mode {
|
||||
"generate" => (mode-generate $args)
|
||||
_ => (mode-format $args)
|
||||
}
|
||||
}
|
||||
|
||||
export def mode-generate [args: record] {
|
||||
generate-config | with-context "generate config"
|
||||
}
|
||||
|
||||
export def mode-format [args: record] {
|
||||
if not ($args.flake? != null xor $args.disko_file? != null) {
|
||||
return {
|
||||
success: false,
|
||||
messages: [
|
||||
{
|
||||
code: (
|
||||
if ($args.flake? == null) { 'ERR_MISSING_ARGUMENTS' } else { 'ERR_TOO_MANY_ARGUMENTS' }
|
||||
)
|
||||
}
|
||||
],
|
||||
context: "validate arguments"
|
||||
}
|
||||
}
|
||||
|
||||
let eval_result = if $args.disko_file? != null {
|
||||
$args.disko_file | eval-disko-file | with-context "evaluate config"
|
||||
} else {
|
||||
$args.flake | eval-flake | with-context "evaluate flake"
|
||||
}
|
||||
|
||||
# TODO: Continue implementation here
|
||||
$eval_result
|
||||
}
|
||||
|
||||
export def main [
|
||||
mode: string@modes, # Mode to use. Allowed values are 'destroy', 'format', 'mount', 'format,mount', 'destroy,format,mount', 'generate'
|
||||
disko_file?: path, # File to read the disko configuration from. Can be a .nix file or a .json file
|
||||
--flake (-f): string # Flake URI to search for the disko configuration
|
||||
]: nothing -> nothing {
|
||||
|
||||
run {
|
||||
mode: $mode,
|
||||
disko_file: $disko_file,
|
||||
flake: $flake
|
||||
}
|
||||
| exit-on-error
|
||||
| to json | print-info
|
||||
}
|
||||
@@ -32,11 +32,13 @@
|
||||
in
|
||||
{
|
||||
disko = pkgs.callPackage ./package.nix { diskoVersion = version; };
|
||||
disko2 = pkgs.callPackage ./package-disko2.nix { diskoVersion = version; };
|
||||
|
||||
# alias to make `nix run` more convenient
|
||||
disko-install = self.packages.${system}.disko.overrideAttrs (_old: {
|
||||
name = "disko-install";
|
||||
});
|
||||
default = self.packages.${system}.disko;
|
||||
default = self.packages.${system}.disko2;
|
||||
|
||||
create-release = pkgs.callPackage ./scripts/create-release.nix { };
|
||||
} // pkgs.lib.optionalAttrs (!pkgs.buildPlatform.isRiscV64) {
|
||||
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
use std log
|
||||
|
||||
# Color definitions. NOTE: Sort them alphabetically when adding new ones!
|
||||
def command [] { ansi cyan_italic } # Commands that were run or can be run
|
||||
def file [] { ansi blue } # File paths
|
||||
def flag [] { ansi green } # Command line flags (like --version or -f)
|
||||
def invalid [] { ansi red } # Invalid values
|
||||
def placeholder [] { ansi magenta_italic } # Values that need to be replaced
|
||||
def value [] { ansi green } # Values that are allowed
|
||||
|
||||
def reset [] { ansi reset } # Shortcut to reset the color
|
||||
|
||||
def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] {
|
||||
let modes_list = $valid_modes
|
||||
| each { |mode| $"(value)($mode)(reset)" }
|
||||
| str join ', '
|
||||
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: $"Invalid mode: (invalid)($mode)(reset)"
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: $"Valid modes are:\n($modes_list)"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def err-arguments-help [] {
|
||||
($"Provide either (placeholder)disko_file(reset) as the second argument or " +
|
||||
$"(flag)--flake(reset)/(flag)-f(reset) (placeholder)flakeref(reset)")
|
||||
}
|
||||
|
||||
def "lookup ERR_MISSING_ARGUMENTS" [] {
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: "Missing arguments!"
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: (err-arguments-help)
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def "lookup ERR_TOO_MANY_ARGUMENTS" [] {
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: "Too many arguments!"
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: (err-arguments-help)
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def "lookup ERR_EVAL_CONFIG_FAILED" [ args, stderr ] {
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: ($"Failed to evaluate disko config with args (invalid)($args)(reset)!\n" +
|
||||
$"Output from `(command)nix eval(reset)`: ($stderr)")
|
||||
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def "lookup ERR_FILE_NOT_FOUND" [ path ] {
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: ($"File (file)($path)(reset) does not exist.")
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def "lookup ERR_FLAKE_URI_NO_ATTRIBUTE" [ flakeUri ] {
|
||||
[
|
||||
{
|
||||
type: error
|
||||
msg: ($"Flake URI (invalid)($flakeUri)(reset) does not contain an attribute.")
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: $"Append an attribute like (value)#(placeholder)foo(reset) to the flake URI."
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def lookup-message []: [
|
||||
record<code: string> -> list<record<type: string, msg: string>>
|
||||
record<code: string, details: record<any>> -> list<record<type: string, msg: string>>
|
||||
] {
|
||||
let code = $in.code
|
||||
let details = $in.details? # Not all errors have details
|
||||
match $code {
|
||||
ERR_INVALID_MODE => (lookup ERR_INVALID_MODE $details.mode $details.valid_modes)
|
||||
ERR_MISSING_ARGUMENTS => (lookup ERR_MISSING_ARGUMENTS)
|
||||
ERR_TOO_MANY_ARGUMENTS => (lookup ERR_TOO_MANY_ARGUMENTS)
|
||||
ERR_EVAL_CONFIG_FAILED => (lookup ERR_EVAL_CONFIG_FAILED $details.args $details.stderr)
|
||||
ERR_FILE_NOT_FOUND => (lookup ERR_FILE_NOT_FOUND $details.path)
|
||||
ERR_FLAKE_URI_NO_ATTRIBUTE => (lookup ERR_FLAKE_URI_NO_ATTRIBUTE $details.flakeUri)
|
||||
_ => [
|
||||
{
|
||||
type: error
|
||||
msg: $"Unknown error code: (invalid)($code)(reset)."
|
||||
}
|
||||
{
|
||||
type: info
|
||||
msg: $"The following details were provided:\n($details)"
|
||||
}
|
||||
{
|
||||
type: help
|
||||
msg: $"Please report this issue, this should never happen!"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
def render-message []: record<type: string, msg: string> -> nothing {
|
||||
let type = $in.type
|
||||
let msg_lines = $in.msg | lines
|
||||
|
||||
let bg_color = match $type {
|
||||
error => (ansi bg_red)
|
||||
help => (ansi bg_light_magenta)
|
||||
info => (ansi bg_green)
|
||||
}
|
||||
let color = match $type {
|
||||
error => (ansi red)
|
||||
help => (ansi light_magenta)
|
||||
info => (ansi green)
|
||||
}
|
||||
let title = match $type {
|
||||
error => "ERROR"
|
||||
help => " HELP"
|
||||
info => " INFO"
|
||||
}
|
||||
|
||||
if ($msg_lines | length) == 1 {
|
||||
print $" ($bg_color) ($title): (reset) ($msg_lines.0)"
|
||||
return
|
||||
}
|
||||
|
||||
print $"($color)╭─($bg_color) ($title): (reset) ($msg_lines.0)"
|
||||
|
||||
for line in ($msg_lines | skip 1) {
|
||||
print $"($color)│ (reset)($line)"
|
||||
}
|
||||
|
||||
print $"($color)╰─────────(reset)"
|
||||
}
|
||||
|
||||
def print-message []: record -> nothing {
|
||||
$in | lookup-message | each { render-message }
|
||||
}
|
||||
|
||||
export def exit-on-error []: [
|
||||
record<success: bool, context: string, value: any> -> any
|
||||
record<success: bool, context: string, messages: list<record<code: string>>, context: string> -> nothing
|
||||
record<success: bool, context: string, messages: list<record<code: string, details: record<any>>>> -> nothing
|
||||
] {
|
||||
let result = $in
|
||||
if $result.success {
|
||||
log debug $"Success: ($result.context)"
|
||||
log debug $"Return value: ($result.value)"
|
||||
return $result.value
|
||||
} else {
|
||||
log debug $"Failure: ($result.context)"
|
||||
}
|
||||
|
||||
for message in $in.messages {
|
||||
$message | print-message
|
||||
}
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Info and help messages may be printed directly, but errors not! That's why we don't have p-err here.
|
||||
export def print-info []: string -> nothing {
|
||||
{ type: info, msg: $in } | render-message
|
||||
}
|
||||
export def print-help []: string -> nothing {
|
||||
{ type: help, msg: $in } | render-message
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
, lib ? pkgs.lib
|
||||
, flake ? null
|
||||
, flakeAttr ? null
|
||||
, diskoFile ? null
|
||||
, rootMountPoint ? "/mnt"
|
||||
, ...
|
||||
}@args:
|
||||
let
|
||||
disko = import ./. {
|
||||
inherit rootMountPoint;
|
||||
inherit lib;
|
||||
};
|
||||
|
||||
flake' = (builtins.getFlake flake);
|
||||
|
||||
hasDiskoFile = diskoFile != null;
|
||||
|
||||
hasFlakeDiskoConfig = lib.hasAttrByPath [ "diskoConfigurations" flakeAttr ] flake';
|
||||
|
||||
hasFlakeDiskoModule =
|
||||
lib.hasAttrByPath [ "nixosConfigurations" flakeAttr "config" "disko" "devices" ] flake';
|
||||
|
||||
diskFormat =
|
||||
let
|
||||
diskoConfig =
|
||||
if hasDiskoFile then
|
||||
import diskoFile
|
||||
else
|
||||
flake'.diskoConfigurations.${flakeAttr};
|
||||
in
|
||||
if builtins.isFunction diskoConfig then
|
||||
diskoConfig ({ inherit lib; } // args)
|
||||
else
|
||||
diskoConfig;
|
||||
|
||||
evaluatedConfig =
|
||||
if hasDiskoFile || hasFlakeDiskoConfig then
|
||||
disko.eval diskFormat
|
||||
else if (lib.traceValSeq hasFlakeDiskoModule) then
|
||||
flake'.nixosConfigurations.${flakeAttr}
|
||||
else
|
||||
(builtins.abort "couldn't find `diskoConfigurations.${flakeAttr}` or `nixosConfigurations.${flakeAttr}.config.disko.devices`");
|
||||
|
||||
diskoConfig = evaluatedConfig.config.disko.devices;
|
||||
|
||||
finalConfig = lib.filterAttrsRecursive (name: value: !lib.hasPrefix "_" name) diskoConfig;
|
||||
in
|
||||
finalConfig
|
||||
@@ -0,0 +1,64 @@
|
||||
use libexec-dir.nu
|
||||
|
||||
use std log
|
||||
|
||||
alias nix = ^nix --extra-experimental-features nix-command --extra-experimental-features flakes
|
||||
alias nix-eval-expr = nix eval --impure --json --expr
|
||||
|
||||
def eval-config [args: record]: nothing -> record {
|
||||
let eval_config_nix = (libexec-dir) + "/lib/eval-config.nix"
|
||||
|
||||
do { nix-eval-expr $"import ($eval_config_nix) \(builtins.fromJSON ''($args | to json -r)'')" }
|
||||
| complete
|
||||
| if $in.exit_code != 0 {
|
||||
{
|
||||
success: false
|
||||
messages: [ { code: ERR_EVAL_CONFIG_FAILED, details: { args: $args, stderr: $in.stderr } } ]
|
||||
}
|
||||
} else {
|
||||
$in.stdout
|
||||
| from json
|
||||
| {
|
||||
success: true
|
||||
value: $in
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export def eval-disko-file []: path -> record {
|
||||
let config = $in
|
||||
|
||||
if not ($config | path exists) {
|
||||
return {
|
||||
success: false
|
||||
messages: [ { code: ERR_FILE_NOT_FOUND, details: { path: $config } } ]
|
||||
}
|
||||
}
|
||||
|
||||
# If the file is valid JSON, parse and return it
|
||||
open $config | try { into record } | if $in != null {
|
||||
return {
|
||||
success: true
|
||||
value: $in
|
||||
}
|
||||
}
|
||||
|
||||
eval-config { diskoFile: $config }
|
||||
}
|
||||
|
||||
export def eval-flake []: string -> record {
|
||||
let flakeUri = $in
|
||||
let parseResult = $flakeUri | parse "{flake}#{flakeAttr}" | if not ( $in | is-empty ) {
|
||||
$in
|
||||
} else {
|
||||
return {
|
||||
success: false
|
||||
messages: [ { code: ERR_FLAKE_URI_NO_ATTRIBUTE, details: { flakeUri: $flakeUri } } ]
|
||||
}
|
||||
}
|
||||
|
||||
let flake = $parseResult.0.flake | if ($in | path exists) { $in | path expand } else { $in }
|
||||
let flakeAttr = $parseResult.0.flakeAttr
|
||||
|
||||
eval-config { flake: $flake, flakeAttr: $flakeAttr }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export def main []: nothing -> path {
|
||||
# $env.FILE_PWD contains the file of the script (disko.nu), not this module
|
||||
let libexec_dir = $env.FILE_PWD
|
||||
$libexec_dir
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export use eval-config.nu [eval-disko-file eval-flake]
|
||||
export use errors.nu [exit-on-error print-info print-help]
|
||||
export use types [generate-config]
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
export def generate-config [partition: record] {
|
||||
assert ($partition.type == 'part') $'BUG! filesystem generate-config called with non-partition: ($partition)'
|
||||
|
||||
{
|
||||
type: 'filesystem',
|
||||
format: $partition.fstype,
|
||||
mountpoint: $partition.mountpoint,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
use std assert
|
||||
use std log
|
||||
|
||||
use filesystem.nu
|
||||
|
||||
def add-type-if-required [partition: record]: record -> record {
|
||||
let config = $in
|
||||
|
||||
let type = match ($partition.parttype) {
|
||||
'c12a7328-f81f-11d2-ba4b-00a0c93ec93b' => 'EF00' # EFI System
|
||||
'21686148-6449-6e6f-744e-656564454649' => 'EF02' # BIOS boot
|
||||
_ => null
|
||||
}
|
||||
|
||||
if $type == null {
|
||||
$config
|
||||
} else {
|
||||
$config | insert type $type
|
||||
}
|
||||
}
|
||||
|
||||
def generate-identifier [partition: record]: record -> string {
|
||||
if not (partition.uuid | is-empty) {
|
||||
$'UUID:($partition.uuid)...'
|
||||
} else {
|
||||
$'PARTUUID:($partition.partuuid)'
|
||||
}
|
||||
}
|
||||
|
||||
def generate-content [partition: record]: record -> record {
|
||||
match ($partition.fstype) {
|
||||
# Add filesystems that are more complicated than mkfs
|
||||
_ => (filesystem generate-config $partition)
|
||||
}
|
||||
}
|
||||
|
||||
export def generate-config [device: record]: nothing -> record {
|
||||
assert ($device.pttype == 'gpt') $'BUG! gpt generate-config called with non-gpt device: ($device)'
|
||||
|
||||
log debug $'Generating config for GPT device ($device.path)'
|
||||
|
||||
let partitions = $device.children
|
||||
| each { |part|
|
||||
{
|
||||
($'UUID:($part.uuid)'): (
|
||||
{
|
||||
size: $part.size,
|
||||
content: (generate-content $part)
|
||||
}
|
||||
| add-type-if-required $part
|
||||
)
|
||||
}
|
||||
}
|
||||
| into record
|
||||
|
||||
{
|
||||
type: 'gpt',
|
||||
partitions: $partitions
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export def print-disks []: record -> nothing {
|
||||
$in | each { print }
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
export use gpt.nu
|
||||
|
||||
# To see what other fields are available in the lsblk output and what
|
||||
# sort of values you can expect from them, run:
|
||||
# lsblk -O | less -S
|
||||
const lsblk_output_fields = [
|
||||
ID-LINK,
|
||||
FSTYPE,
|
||||
FSSIZE,
|
||||
FSUSE%,
|
||||
KNAME,
|
||||
LABEL,
|
||||
MODEL,
|
||||
PARTFLAGS,
|
||||
PARTLABEL,
|
||||
PARTN,
|
||||
PARTTYPE,
|
||||
PARTTYPENAME,
|
||||
PARTUUID, # The UUID used for /dev/disk/by-partuuid
|
||||
PATH,
|
||||
PHY-SEC,
|
||||
PTTYPE,
|
||||
REV,
|
||||
SERIAL,
|
||||
SIZE,
|
||||
START,
|
||||
MOUNTPOINT, # Canonical mountpoint
|
||||
MOUNTPOINTS, # All mountpoints, including e.g. bind mounts
|
||||
TYPE,
|
||||
UUID, # The UUID used for /dev/disk/by-uuid, if available
|
||||
]
|
||||
|
||||
export def list-block-devices []: nothing -> table {
|
||||
^lsblk --output ($lsblk_output_fields | str join ',') --json --tree
|
||||
| from json
|
||||
| $in.blockdevices
|
||||
}
|
||||
|
||||
def generate-content [device: record] nothing -> record {
|
||||
match $device.pttype {
|
||||
gpt => (gpt generate-config $device)
|
||||
_ => { errors: [ { code: ERR_UNSUPPORTED_PARTITION_TABLE_TYPE, details: { pttype: $device.pttype } } ] }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export def generate-config [block_devices?: table]: nothing -> record {
|
||||
let block_devices = $block_devices | default (list-block-devices)
|
||||
|
||||
let disks = $block_devices
|
||||
| each { |device|
|
||||
{
|
||||
($'MODEL:($device.model),SN:($device.serial)'):{
|
||||
device: $device.kname,
|
||||
type: $device.type,
|
||||
content: (generate-content $device)
|
||||
}
|
||||
}
|
||||
}
|
||||
| into record
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: {
|
||||
disk: $disks
|
||||
# TODO: Add lvm_vg, mdadm, nodev and zpool
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export def print-disks []: record -> nothing {
|
||||
$in | each { print }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{ stdenvNoCC, makeWrapper, lib, path, nix, coreutils, nixos-install-tools, binlore, diskoVersion }:
|
||||
|
||||
let
|
||||
shareDir = "share/disko2";
|
||||
self = stdenvNoCC.mkDerivation (finalAttrs: {
|
||||
name = "disko2";
|
||||
src = ./.;
|
||||
nativeBuildInputs = [
|
||||
makeWrapper
|
||||
];
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin $out/${shareDir}
|
||||
cp -r default.nix disk-deactivate lib $out/${shareDir}
|
||||
cp disko2 $out/bin/
|
||||
|
||||
sed -ie "s|libexec_dir = .*|libexec_dir = '$out/${shareDir}'|" $out/${shareDir}/lib/libexec-dir.nu
|
||||
sed -ie "s|use lib|use \"$out/${shareDir}/lib\"|" $out/bin/disko2
|
||||
chmod 755 "$out/bin/disko2"
|
||||
wrapProgram "$out/bin/disko2" \
|
||||
--set DISKO_VERSION "${diskoVersion}" \
|
||||
--prefix PATH : ${lib.makeBinPath [ nix coreutils nixos-install-tools ]} \
|
||||
--prefix NIX_PATH : "nixpkgs=${path}"
|
||||
'';
|
||||
# Otherwise resholve thinks that disko and disko-install might be able to execute their arguments
|
||||
passthru.binlore.out = binlore.synthesize self ''
|
||||
execer cannot bin/.disko2-wrapped
|
||||
'';
|
||||
meta = with lib; {
|
||||
description = "Format disks with nix-config";
|
||||
homepage = "https://github.com/nix-community/disko";
|
||||
license = licenses.mit;
|
||||
maintainers = with maintainers; [ lassulus ];
|
||||
platforms = platforms.linux;
|
||||
mainProgram = finalAttrs.name;
|
||||
};
|
||||
});
|
||||
in
|
||||
self
|
||||
Reference in new issue
Block a user