From 5a1b1cd8c1d69592902f8e752c6fe7dac35e230b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 8 Sep 2024 17:09:01 +0200 Subject: [PATCH] Switch to named flakes --- flake.nix | 5 ++-- lib/eval-attribute.nix | 15 +++++++++++ .../machines/manuel/DerGeret/Arch/setup.fish | 2 +- .../manuel/DerGeret/Windows/Setup.ps1 | 2 +- .../manuel/ManuSurface/Arch/setup.fish | 2 +- scripts/Common/OS/install.fish | 2 +- scripts/Common/OS/setup.fish | 2 +- scripts/Common/Scripts/Config.ps1 | 17 +++++++++++- scripts/Common/Scripts/Operations.ps1 | 9 ++----- scripts/Common/Scripts/config.fish | 11 +++++--- scripts/Common/Scripts/eval-flake.fish | 26 +++++++++++++++++++ scripts/Common/Software/rclone/main.fish | 4 +-- scripts/Windows/OS/Setup.ps1 | 12 ++++----- scripts/Windows/Scripts/PowerManagement.ps1 | 2 +- winiso/deploy.fish | 8 +++--- 15 files changed, 88 insertions(+), 31 deletions(-) create mode 100644 lib/eval-attribute.nix create mode 100755 scripts/Common/Scripts/eval-flake.fish diff --git a/flake.nix b/flake.nix index 72f937ec..4e681c6d 100644 --- a/flake.nix +++ b/flake.nix @@ -34,9 +34,8 @@ }; })) // { valhalla = { - "DerGeret Windows" = import ./profiles/DerGeret/config.nix; - "der-geret Arch Linux" = import ./profiles/DerGeret/Arch/config.nix; - "manu-surface Arch Linux" = import ./profiles/ManuSurface/Arch/config.nix; + DerGeret = import ./profiles/machines/manuel/DerGeret/Arch/config.nix; + ManuSurface = import ./profiles/machines/manuel/ManuSurface/Arch/config.nix; }; }; } diff --git a/lib/eval-attribute.nix b/lib/eval-attribute.nix new file mode 100644 index 00000000..70674c73 --- /dev/null +++ b/lib/eval-attribute.nix @@ -0,0 +1,15 @@ +let + inherit (nixpkgs) lib; + nixpkgs = import { config = {}; overlay = []; }; + property = (builtins.getEnv "PROPERTY"); + processor = if (builtins.stringLength property > 0) + then + (_: lib.attrsets.getAttrFromPath (lib.strings.splitString "." property) _) + else + (_: _); +in + _: processor (lib.evalModules { + modules = [ + _ + ]; + }).config diff --git a/profiles/machines/manuel/DerGeret/Arch/setup.fish b/profiles/machines/manuel/DerGeret/Arch/setup.fish index 5d041ea2..f6ddde5f 100755 --- a/profiles/machines/manuel/DerGeret/Arch/setup.fish +++ b/profiles/machines/manuel/DerGeret/Arch/setup.fish @@ -6,7 +6,7 @@ begin echo "$dir/install.fish" end - CONFIG_MODULE="$dir/config.nix" \ + CONFIG_NAME="DerGeret" \ ARCH_HOSTNAME="der-geret" \ USER_DISPLAYNAME="Manuel Thalmann" \ source "$(status dirname)/../../../../../scripts/Arch/OS/setup.fish" diff --git a/profiles/machines/manuel/DerGeret/Windows/Setup.ps1 b/profiles/machines/manuel/DerGeret/Windows/Setup.ps1 index bd4ab8e9..5b16249e 100644 --- a/profiles/machines/manuel/DerGeret/Windows/Setup.ps1 +++ b/profiles/machines/manuel/DerGeret/Windows/Setup.ps1 @@ -1,6 +1,6 @@ #!/bin/pwsh $env:WIN_COMPUTER_NAME = "DerGeret"; $env:SETUP_SCRIPT_NAME = "$PSScriptRoot/Restore.ps1"; -$env:CONFIG_MODULE = "$PSScriptRoot/../config.nix"; +$env:CONFIG_NAME = $env:WIN_COMPUTER_NAME; . "$PSScriptRoot/../../../../../scripts/Windows/OS/Setup.ps1"; diff --git a/profiles/machines/manuel/ManuSurface/Arch/setup.fish b/profiles/machines/manuel/ManuSurface/Arch/setup.fish index b613bc04..0fee01e0 100755 --- a/profiles/machines/manuel/ManuSurface/Arch/setup.fish +++ b/profiles/machines/manuel/ManuSurface/Arch/setup.fish @@ -11,7 +11,7 @@ begin echo "$dir/install.fish" end - CONFIG_MODULE="$(status dirname)/config.nix" \ + CONFIG_NAME="ManuSurface" \ ARCH_HOSTNAME="manu-surface" \ USER_DISPLAYNAME="Manuel Thalmann" \ source "$(status dirname)/../../../../../scripts/Arch/OS/setup.fish"; diff --git a/scripts/Common/OS/install.fish b/scripts/Common/OS/install.fish index 24767546..644538d0 100755 --- a/scripts/Common/OS/install.fish +++ b/scripts/Common/OS/install.fish @@ -24,7 +24,7 @@ if [ (id -u) -eq 0 ] echo "$name ALL=(ALL:ALL) NOPASSWD: ALL" end > "$sudoConfig" - and sudo --preserve-env="CONFIG_MODULE" --user "$name" "$INSTALLER_SCRIPT" + and sudo --preserve-env="CONFIG_NAME" --user "$name" "$INSTALLER_SCRIPT" rm "$sudoConfig" userdel -r "$name" else diff --git a/scripts/Common/OS/setup.fish b/scripts/Common/OS/setup.fish index a8d3eea5..75d19ccd 100755 --- a/scripts/Common/OS/setup.fish +++ b/scripts/Common/OS/setup.fish @@ -39,7 +39,7 @@ function runSetup function wrapScript -S printf %s\n \ "cd $PROJECT_CLONE_ROOT" \ - "export CONFIG_MODULE=$(string escape (getCloneFile "$CONFIG_MODULE"))" \ + "export CONFIG_NAME=$(string escape (getCloneFile "$CONFIG_NAME"))" \ "$argv" end diff --git a/scripts/Common/Scripts/Config.ps1 b/scripts/Common/Scripts/Config.ps1 index 70aab37e..1790fed0 100644 --- a/scripts/Common/Scripts/Config.ps1 +++ b/scripts/Common/Scripts/Config.ps1 @@ -115,16 +115,29 @@ $null = New-Module { $scriptPath = "$PSScriptRoot/../../Common/Scripts/config.fish"; - if ($env:CONFIG_MODULE) { + if ($env:CONFIG_NAME) { $output = & { if (-not $IsWindows) { $escapedPath = (fish -c 'string escape $argv' "$scriptPath"); fish -c ". $escapedPath; $Script"; } else { + $cleanup = { }; + $projectRoot = "$PSScriptRoot/../../.."; + $archisoDir = "$projectRoot/archiso"; + function fish { wsl --shell-type login -- nix --extra-experimental-features "nix-command flakes" run nixpkgs`#fish -- $args } + if (Test-Path -PathType Container "$archisoDir") { + $git = { + git -C "$projectRoot" -c safe.directory="$("$(Resolve-Path $projectRoot)".Replace("\", "/"))" @args; + }; + + & $git rm -r --cached "$archisoDir" *> $null; + $cleanup = { & $git restore --staged "$archisoDir" }; + } + $output = fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script"; if (-not $?) { @@ -132,6 +145,8 @@ $null = New-Module { } else { $output; } + + & $cleanup *> $null; } } diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index 047157e6..1d7a7079 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -80,12 +80,7 @@ $null = New-Module { $Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire'; if ($IsWindows) { - $env:WSLENV = "CONFIG_MODULE/p"; - - if ($env:CONFIG_MODULE) { - $env:CONFIG_MODULE = Resolve-Path $env:CONFIG_MODULE; - } - + $env:WSLENV = "CONFIG_NAME"; New-Alias -Force "sudo" gsudo; } @@ -150,7 +145,7 @@ $null = New-Module { Start-Sleep 0.1; } - foreach ($name in @("CONFIG_MODULE", "INSTALLER_SCRIPT")) { + foreach ($name in @("INSTALLER_SCRIPT")) { $variable = Get-Item "Env:\$name"; $path = Join-Path ` diff --git a/scripts/Common/Scripts/config.fish b/scripts/Common/Scripts/config.fish index 88f551ae..9f777c34 100755 --- a/scripts/Common/Scripts/config.fish +++ b/scripts/Common/Scripts/config.fish @@ -1,11 +1,16 @@ #!/bin/env fish function isConfigured -S - set -q "$CONFIG_MODULE" + set -q CONFIG_NAME +end + +function getProfiles -S + source "$(status dirname)/eval-flake.fish"; + evalFlake "" "" --apply "builtins.attrNames" --json end function getConfig -S -a property - source "$(status dirname)/eval-module.fish" - evalModule "$CONFIG_MODULE" "$property" $argv[2..] + source "$(status dirname)/eval-flake.fish" + evalFlake "$CONFIG_NAME" "$property" $argv[2..] end function getAttributes -S -a property diff --git a/scripts/Common/Scripts/eval-flake.fish b/scripts/Common/Scripts/eval-flake.fish new file mode 100755 index 00000000..f3f22600 --- /dev/null +++ b/scripts/Common/Scripts/eval-flake.fish @@ -0,0 +1,26 @@ +#!/bin/env fish +function evalFlake --argument-names config property + set -l argv $argv[3..] + argparse --ignore-unknown "apply=" "json" -- $argv + + if [ -z "$_flag_json" ] + set -a argv --raw + else + set -a argv --json + end + + if [ -z "$_flag_apply" ] + set _flag_apply "_: _" + end + + if [ -n "$config" ] + set config ".$config" + set _flag_apply "_: ($_flag_apply) ((import $(status dirname)/../../../lib/eval-attribute.nix) _)" + end + + PROPERTY="$property" \ + nix eval --impure --extra-experimental-features "nix-command flakes" \ + --apply "$_flag_apply" \ + "$(realpath (status dirname))/../../..#valhalla$config" \ + $argv +end diff --git a/scripts/Common/Software/rclone/main.fish b/scripts/Common/Software/rclone/main.fish index 7707656c..14c66d13 100755 --- a/scripts/Common/Software/rclone/main.fish +++ b/scripts/Common/Software/rclone/main.fish @@ -26,7 +26,7 @@ begin "Terminal=false" ( printf "%s " \ "Exec=env" \ - "CONFIG_MODULE=$(echo "$CONFIG_MODULE" | string escape)" \ + "CONFIG_NAME=$(echo "$CONFIG_NAME" | string escape)" \ "konsole -e fish $(realpath (status filename) | string escape) userConfig" ) end | sudo -u "$name" tee "$entryPoint" > /dev/null @@ -36,7 +36,7 @@ begin rm "$entryPoint" rm ~"$name"/.config/plasma-welcomerc else if [ -n "$name" ] && [ "$name" != "$USER" ] - sudo -Eu "$name" CONFIG_MODULE="$CONFIG_MODULE" fish "$(status filename)" userConfig + sudo -Eu "$name" CONFIG_NAME="$CONFIG_NAME" fish "$(status filename)" userConfig else systemctl --user enable rclone.service echo "Please complete the setup of your rclone configurations!" diff --git a/scripts/Windows/OS/Setup.ps1 b/scripts/Windows/OS/Setup.ps1 index b2b6f99b..ab09813e 100644 --- a/scripts/Windows/OS/Setup.ps1 +++ b/scripts/Windows/OS/Setup.ps1 @@ -2,16 +2,16 @@ . "$PSScriptRoot/../../Common/Scripts/Scripting.ps1"; function Start-Setup { + param($ConfigurationName) . "$PSScriptRoot/../../Common/Scripts/Scripting.ps1"; $Global:InformationPreference = "Continue"; $Global:ErrorActionPreference = "Inquire"; + $env:CONFIG_NAME ??= $ConfigurationName; $null = $env:WIN_COMPUTER_NAME; $null = $env:SETUP_SCRIPT_NAME; - $null = $env:CONFIG_MODULE; - $env:WSLENV = "CONFIG_MODULE/p"; - - $valhallaConfig = ConvertFrom-Json (Get-Content "$env:CONFIG_MODULE.json"); + $env:WSLENV = "CONFIG_NAME"; + $valhallaConfig = ConvertFrom-Json (Get-Content "$PSScriptRoot/../../../.config/$env:CONFIG_NAME.json"); [xml]$unattendedConfig = [xml]::new(); $unattendedConfig.PreserveWhitespace = $true; @@ -233,7 +233,7 @@ function Start-Setup { ($env:DEBUG ? "`$env:DEBUG = $([int]$env:DEBUG);" : "") + "`$env:PWSH_PATH = $(Get-PathInjection $env:PWSH_PATH);" + "`$env:INSTALLER_SCRIPT = $(Get-ScriptPathInjection $env:SETUP_SCRIPT_NAME);" + - "`$env:CONFIG_MODULE = $(Get-ScriptPathInjection $env:CONFIG_MODULE);" + + "`$env:CONFIG_NAME = $(ConvertTo-Injection $env:CONFIG_NAME);" + "& (Join-Path `$env:PWSH_PATH pwsh) `$env:INSTALLER_SCRIPT;") ` -Description "Install PowerShell Core and git and run setup script"; @@ -264,4 +264,4 @@ function Start-Setup { & "$SETUP_DRIVE\setup.exe" /Unattend:$unattendedConfigFile; } -Start-Setup +Start-Setup @args; diff --git a/scripts/Windows/Scripts/PowerManagement.ps1 b/scripts/Windows/Scripts/PowerManagement.ps1 index ee4f38b3..f8872075 100644 --- a/scripts/Windows/Scripts/PowerManagement.ps1 +++ b/scripts/Windows/Scripts/PowerManagement.ps1 @@ -55,7 +55,7 @@ $null = New-Module { ($env:DEBUG ? "`$env:DEBUG = $([int]$env:DEBUG);" : "") + ($env:BACKUP_ARCHIVE ? "`$env:BACKUP_ARCHIVE = $(ConvertTo-Injection (Resolve-Path $env:BACKUP_ARCHIVE));" : "") + "`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection (Resolve-Path $env:INSTALLER_SCRIPT));" + - "`$env:CONFIG_MODULE = $(ConvertTo-Injection (Resolve-Path $env:CONFIG_MODULE));" + + "`$env:CONFIG_NAME = $(ConvertTo-Injection $env:CONFIG_NAME);" + "& `$env:INSTALLER_SCRIPT;"; } diff --git a/winiso/deploy.fish b/winiso/deploy.fish index 3a9bbd3f..8f4c714e 100755 --- a/winiso/deploy.fish +++ b/winiso/deploy.fish @@ -209,9 +209,11 @@ begin begin source "$dir/../scripts/Common/Scripts/config.fish" - find "$projectPath/profiles" -type f -name "*.nix" | while read -l file - set -l CONFIG_MODULE "$file" - getConfig "valhalla.windows.config" --json > "$file.json" + for name in (getProfiles | jq '.[]' --raw-output0 | string split0) + set -l CONFIG_NAME "$name" + set -l file "$projectPath/.config/$name.json" + mkdir -p (dirname $file) + getConfig "valhalla.windows.config" --json > "$file" end end