Create a script for managing nix

This commit is contained in:
Manuel Thalmann 2024-11-01 14:13:41 +01:00
parent 1ded9f42bb
commit 94b22720b6
4 changed files with 41 additions and 23 deletions

View file

@ -1,31 +1,24 @@
#!/bin/env fish #!/bin/env fish
begin 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 projectName archiso-valhalla
set -l overlayDir (mktemp -d) set -l overlayDir (mktemp -d)
set -l upperDir (mktemp -d) set -l upperDir (mktemp -d)
set -l workDir (mktemp -d) set -l workDir (mktemp -d)
set -l cacheRoot ~/".cache/$projectName"
set -l nixCache "$cacheRoot/nixpkgs/$nixVersion"
set -l root airootfs set -l root airootfs
set -l rootHome "$overlayDir/$root/root" set -l rootHome "$overlayDir/$root/root"
set -l profileDir "/mnt/$projectName" set -l profileDir "/mnt/$projectName"
set -l projectDir "$rootHome/PortValhalla" 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" mkdir -p "$rootHome"
and fish "$(status dirname)/../../scripts/lib/copy-repo.fish" "$projectDir" and fish "$(status dirname)/../../scripts/lib/copy-repo.fish" "$projectDir"
downloadNixPkgs
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
and sudo mount --mkdir -t overlay overlay -o lowerdir=.:"$overlayDir",upperdir="$upperDir",workdir="$workDir" "$profileDir" 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" and sudo mkarchiso $argv "$profileDir"
sudo umount "$nixDir" sudo umount "$nixDir"
and sudo umount "$profileDir" and sudo umount "$profileDir"

View file

@ -6,8 +6,8 @@ source "$dir/../../lib/hooks.fish"
if [ (id -u) -eq 0 ] if [ (id -u) -eq 0 ]
source "$dir/../../lib/config.fish" source "$dir/../../lib/config.fish"
source "$dir/../../lib/nix.fish"
set -l sudoConfig "/etc/sudoers.d/PortValhalla" set -l sudoConfig "/etc/sudoers.d/PortValhalla"
set -l channelDir /nix/var/nix/profiles/per-user/root/channels/nixpkgs
rm ~/.bash_profile rm ~/.bash_profile
if ! git status -C (status dirname) &> /dev/null if ! git status -C (status dirname) &> /dev/null
@ -20,13 +20,8 @@ if [ (id -u) -eq 0 ]
and set -x CONFIG_NAME "$config" and set -x CONFIG_NAME "$config"
end 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`.' 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 and tmux new-session $cmdline
else else
set -l name (getOSConfig setupUser.name) set -l name (getOSConfig setupUser.name)
@ -51,7 +46,8 @@ if [ (id -u) -eq 0 ]
and sudo --preserve-env --set-home --user "$name" $cmdline and sudo --preserve-env --set-home --user "$name" $cmdline
rm "$sudoConfig" rm "$sudoConfig"
userdel -rf "$name" userdel -rf "$name"
rm -rf "$channelDir" uninstallNixPkgs
read -P "finished. press enter"
end end
else else
if not type -q getDeploymentScript if not type -q getDeploymentScript
@ -95,7 +91,7 @@ else
echo "Cleaning installation scripts..." echo "Cleaning installation scripts..."
set -l projectPath (realpath "$(status dirname)/../../..") set -l projectPath (realpath "$(status dirname)/../../..")
cd (dirname "$projectPath") cd (dirname "$projectPath")
sudo rm -rf "$projectPath" # sudo rm -rf "$projectPath"
end end
and echo "The installation finished successfully!" and echo "The installation finished successfully!"

View file

@ -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"

26
scripts/lib/nix.fish Normal file
View file

@ -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