From 94b22720b6614c5a85bf4e01ad019f269bd71f48 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 1 Nov 2024 14:13:41 +0100 Subject: [PATCH] Create a script for managing `nix` --- archiso/scripts/build.fish | 19 ++++++------------- scripts/Common/OS/install.fish | 14 +++++--------- scripts/lib/config.fish | 5 ++++- scripts/lib/nix.fish | 26 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 scripts/lib/nix.fish diff --git a/archiso/scripts/build.fish b/archiso/scripts/build.fish index a21b0832..92454d8c 100755 --- a/archiso/scripts/build.fish +++ b/archiso/scripts/build.fish @@ -1,31 +1,24 @@ #!/bin/env fish begin - source "$(status dirname)/../../scripts/lib/config.fish" + set -l dir (status dirname) + source "$dir/../../scripts/lib/config.fish" + source "$dir/../../scripts/lib/nix.fish" set -l projectName archiso-valhalla set -l overlayDir (mktemp -d) set -l upperDir (mktemp -d) set -l workDir (mktemp -d) - set -l cacheRoot ~/".cache/$projectName" - set -l nixCache "$cacheRoot/nixpkgs/$nixVersion" set -l root airootfs set -l rootHome "$overlayDir/$root/root" set -l profileDir "/mnt/$projectName" set -l projectDir "$rootHome/PortValhalla" - set -l nixDir "$profileDir/$root/nix/var/nix/profiles/per-user/root/channels/nixpkgs" + set -l nixDir "$profileDir/$root$nixPkgsDir" mkdir -p "$rootHome" and fish "$(status dirname)/../../scripts/lib/copy-repo.fish" "$projectDir" - - and begin - if [ ! -d "$nixCache" ] - mkdir -p "$nixCache" - and git clone https://github.com/NixOS/nixpkgs.git --depth=1 -b "$nixVersion" "$nixCache" - and rm -rf "$nixCache/.git" - end - end + downloadNixPkgs and sudo mount --mkdir -t overlay overlay -o lowerdir=.:"$overlayDir",upperdir="$upperDir",workdir="$workDir" "$profileDir" - and sudo mount --mkdir --bind "$nixCache" "$nixDir" + and sudo mount --mkdir --bind "$nixPkgsCache" "$nixDir" and sudo mkarchiso $argv "$profileDir" sudo umount "$nixDir" and sudo umount "$profileDir" diff --git a/scripts/Common/OS/install.fish b/scripts/Common/OS/install.fish index 4429fe18..5719c39c 100755 --- a/scripts/Common/OS/install.fish +++ b/scripts/Common/OS/install.fish @@ -6,8 +6,8 @@ source "$dir/../../lib/hooks.fish" if [ (id -u) -eq 0 ] source "$dir/../../lib/config.fish" + source "$dir/../../lib/nix.fish" set -l sudoConfig "/etc/sudoers.d/PortValhalla" - set -l channelDir /nix/var/nix/profiles/per-user/root/channels/nixpkgs rm ~/.bash_profile if ! git status -C (status dirname) &> /dev/null @@ -20,13 +20,8 @@ if [ (id -u) -eq 0 ] and set -x CONFIG_NAME "$config" end - if [ ! -d "$channelDir" ] - mkdir -p "$channelDir" - and git clone https://github.com/NixOS/nixpkgs.git --depth=1 -b "$nixVersion" "$channelDir" - and rm -rf "$channelDir/.git" - end - and runHook --force installValhallaDeps 'Please set up a function `installValhallaDeps` for installing `fish`, `git`, `jq`, `nix`, `sudo` and `tmux`.' + installNixPkgs and tmux new-session $cmdline else set -l name (getOSConfig setupUser.name) @@ -51,7 +46,8 @@ if [ (id -u) -eq 0 ] and sudo --preserve-env --set-home --user "$name" $cmdline rm "$sudoConfig" userdel -rf "$name" - rm -rf "$channelDir" + uninstallNixPkgs + read -P "finished. press enter" end else if not type -q getDeploymentScript @@ -95,7 +91,7 @@ else echo "Cleaning installation scripts..." set -l projectPath (realpath "$(status dirname)/../../..") cd (dirname "$projectPath") - sudo rm -rf "$projectPath" + # sudo rm -rf "$projectPath" end and echo "The installation finished successfully!" diff --git a/scripts/lib/config.fish b/scripts/lib/config.fish index 42442e2c..84655f89 100644 --- a/scripts/lib/config.fish +++ b/scripts/lib/config.fish @@ -1 +1,4 @@ -set nixVersion nixos-24.05 +set projectName port-valhalla +set nixPkgsVersion nixos-24.05 +set cacheRoot ~/".cache/$projectName" +set nixPkgsCache "$cacheRoot/nixpkgs/$nixPkgsVersion" diff --git a/scripts/lib/nix.fish b/scripts/lib/nix.fish new file mode 100644 index 00000000..62e74cbd --- /dev/null +++ b/scripts/lib/nix.fish @@ -0,0 +1,26 @@ +begin + set -l dir (status dirname) + set -l config "$dir/config.fish" + set nixPkgsDir "/nix/var/nix/profiles/per-user/root/channels/nixpkgs" + + function downloadNixPkgs -V config + source "$config" + + if [ ! -d "$nixPkgsCache" ] + mkdir -p "$nixPkgsCache" + and git clone https://github.com/NixOS/nixpkgs.git --depth=1 -b "$nixPkgsVersion" "$nixPkgsCache" + and rm -rf "$nixPkgsCache/.git" + end + end + + function installNixPkgs -V config -V nixPkgsDir + source "$config" + downloadNixPkgs + mkdir -p "$nixPkgsDir" + cp -r "$nixPkgsCache"/* "$nixPkgsDir" + end + + function uninstallNixPkgs -V nixPkgsDir + rm -rf "$nixPkgsDir" + end +end