From 228e811dfe56930ed414c4b9e0843e4f537d3cbd Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 3 May 2024 12:57:48 +0200 Subject: move scripts to bin/ --- .dir-locals.el | 2 +- .envrc | 7 +-- bin/darwin-rebuild | 1 + bin/home-manager | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/nixos-rebuild | 1 + darwin-rebuild | 1 - home-manager | 132 --------------------------------------------------- nixos-rebuild | 1 - 8 files changed, 143 insertions(+), 138 deletions(-) create mode 120000 bin/darwin-rebuild create mode 100755 bin/home-manager create mode 120000 bin/nixos-rebuild delete mode 120000 darwin-rebuild delete mode 100755 home-manager delete mode 120000 nixos-rebuild diff --git a/.dir-locals.el b/.dir-locals.el index 5535ffa9..e352b5ac 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -1,4 +1,4 @@ ;;; Directory Local Variables ;;; For more information see (info "(emacs) Directory Variables") -((nil . ((compile-command . "home-manager switch")))) +((nil . ((compile-command . "./bin/home-manager switch")))) diff --git a/.envrc b/.envrc index fa82cfc4..97eccbae 100644 --- a/.envrc +++ b/.envrc @@ -1,7 +1,8 @@ if type -P lorri &>/dev/null; then - eval "$(lorri direnv)" + eval "$(lorri direnv)" else - echo 'while direnv evaluated .envrc, could not find the command "lorri" [https://github.com/nix-community/lorri]' - use flake + echo 'while direnv evaluated .envrc, could not find the command "lorri" [https://github.com/nix-community/lorri]' + use flake fi +PATH_add bin FLAKE=$PWD diff --git a/bin/darwin-rebuild b/bin/darwin-rebuild new file mode 120000 index 00000000..2eaccdef --- /dev/null +++ b/bin/darwin-rebuild @@ -0,0 +1 @@ +home-manager \ No newline at end of file diff --git a/bin/home-manager b/bin/home-manager new file mode 100755 index 00000000..fa4e1df8 --- /dev/null +++ b/bin/home-manager @@ -0,0 +1,136 @@ +#!/usr/bin/env fish + +if not set --query HOME + echo "error: no HOME" + exit 1 +end + +set --function channel_root $HOME/.nix-defexpr/channels +set --function user_nixpkgs $HOME/.nixpkgs + +if set --query XDG_CONFIG_HOME + set --function hm_config_dir $XDG_CONFIG_HOME/home-manager +else + set --function hm_config_dir $HOME/.config/home-manager +end + +for i in 1 + if test ! -d $user_nixpkgs + echo "user nixpkgs folder $user_nixpkgs does not exist, creating it" + mkdir -p $user_nixpkgs + end + if test -L $hm_config_dir -a (path resolve $hm_config_dir) = (path resolve (status basename)) + if test y = (read --nchars=1 --prompt-str="$hm_config_dir already exists as a symlink to the current directory. Remove it [yN]? ") + unlink $hm_config_dir + end + end + if test ! -d $hm_config_dir + echo "user home-manager configuration folder $hm_config_dir does not exist, creating it" + mkdir -p $hm_config_dir + end + if test ! -e $channel_root + if test -L $channel_root + # broken symlink + rm $channel_root + end + echo "channel root $channel_root does not exist, creating it" + mkdir -p $channel_root + else + if test ! -d $channel_root + echo "error: $channel_root is not a directory" + else if test -L $channel_root + echo "error: $channel_root is a symlink, please remove it" + else if path is --invert --perm write $channel_root + echo "error: $channel_root is not writable; might be a symlink" + else if path is --invert --type link $channel_root/* + for p in (path filter --invert --type link $channel_root/*) + # might be a broken symlink + if test -L $p -a ! -e $p + rm $p + else + echo "error: channel $p is not a broken symlink, don't know what to do with it" + end + end + # check again and fail if there are still unknown files + if path is --invert --type link $channel_root/* + echo "error: non-link(s) in $channel_root" + path filter --invert --type link $channel_root/* + else + continue + end + else + continue # with script + end + exit 1 + end +end + +set --function current_script_name (status basename) +switch $current_script_name + case darwin-rebuild + set --local darwin_config_source (path resolve (status dirname)/../system/$hostname.nix) + set --local darwin_config_target $user_nixpkgs/darwin-configuration.nix + if test ! -e $darwin_config_target + ln -s $darwin_config_source $darwin_config_target + end + set --append argv -I darwin-config=$darwin_config_source + case nixos-rebuild + set --local nixos_config_source (path resolve (status dirname)/system/$hostname.nix) + set --local nixos_config_target $user_nixpkgs/configuration.nix + if test ! -e $nixos_config_target + ln -s $nixos_config_source $nixos_config_target + end + if ! fish_is_root_user + set --append argv --use-remote-sudo + end + set --append argv -I nixos-config=$nixos_config_source + case home-manager + set --local hm_config_source (path resolve (status dirname)/../user/$hostname.nix) + set --local hm_config_target $hm_config_dir/home.nix + if test ! -e $hm_config_target + ln -s $hm_config_source $hm_config_target + end + set --export HOME_MANAGER_CONFIG $hm_config_source +end + +set --function old_channels (path basename $channel_root/*) + +for np in $NIX_PATH + echo $np | read --function --delimiter "=" channel new_src_rel + set --local new_src (path resolve $new_src_rel) + + if test $channel = darwin-config + continue + end + if set --local i (contains --index $channel $old_channels) + set --erase old_channels[$i] + end + set --local target $channel_root/$channel + set --local src (path resolve $target) + if test -e $src + if test $src = $new_src + # no need to re-link it + continue + else + unlink $target + end + end + ln -s $new_src $target +end + +for old in $channel_root/$old_channels + rm $old +end + +set --function i (contains --index (status dirname) $PATH) +if test -n $i + set --erase PATH[$i] +end + +set --function cmd $current_script_name $argv + +if set --query IN_NIX_SHELL + eval $cmd +else + nix-shell --run "$cmd" +end diff --git a/bin/nixos-rebuild b/bin/nixos-rebuild new file mode 120000 index 00000000..2eaccdef --- /dev/null +++ b/bin/nixos-rebuild @@ -0,0 +1 @@ +home-manager \ No newline at end of file diff --git a/darwin-rebuild b/darwin-rebuild deleted file mode 120000 index 2eaccdef..00000000 --- a/darwin-rebuild +++ /dev/null @@ -1 +0,0 @@ -home-manager \ No newline at end of file diff --git a/home-manager b/home-manager deleted file mode 100755 index f351b2f3..00000000 --- a/home-manager +++ /dev/null @@ -1,132 +0,0 @@ -#!/usr/bin/env fish - -if not set --query HOME - echo "error: no HOME" - exit 1 -end - -set --function channel_root $HOME/.nix-defexpr/channels -set --function user_nixpkgs $HOME/.nixpkgs - -if set --query XDG_CONFIG_HOME - set --function hm_config_dir $XDG_CONFIG_HOME/home-manager -else - set --function hm_config_dir $HOME/.config/home-manager -end - -for i in 1 - if test ! -d $user_nixpkgs - echo "user nixpkgs folder $user_nixpkgs does not exist, creating it" - mkdir -p $user_nixpkgs - end - if test -L $hm_config_dir -a (path resolve $hm_config_dir) = (path resolve (status basename)) - if test y = (read --nchars=1 --prompt-str="$hm_config_dir already exists as a symlink to the current directory. Remove it [yN]? ") - unlink $hm_config_dir - end - end - if test ! -d $hm_config_dir - echo "user home-manager configuration folder $hm_config_dir does not exist, creating it" - mkdir -p $hm_config_dir - end - if test ! -e $channel_root - if test -L $channel_root - # broken symlink - rm $channel_root - end - echo "channel root $channel_root does not exist, creating it" - mkdir -p $channel_root - else - if test ! -d $channel_root - echo "error: $channel_root is not a directory" - else if test -L $channel_root - echo "error: $channel_root is a symlink, please remove it" - else if path is --invert --perm write $channel_root - echo "error: $channel_root is not writable; might be a symlink" - else if path is --invert --type link $channel_root/* - for p in (path filter --invert --type link $channel_root/*) - # might be a broken symlink - if test -L $p -a ! -e $p - rm $p - else - echo "error: channel $p is not a broken symlink, don't know what to do with it" - end - end - # check again and fail if there are still unknown files - if path is --invert --type link $channel_root/* - echo "error: non-link(s) in $channel_root" - path filter --invert --type link $channel_root/* - else - continue - end - else - continue # with script - end - exit 1 - end -end - -set --function current_script_name (status basename) -switch $current_script_name - case darwin-rebuild - set --local darwin_config_source (path resolve (status dirname)/system/$hostname.nix) - set --local darwin_config_target $user_nixpkgs/darwin-configuration.nix - if test ! -e $darwin_config_target - ln -s $darwin_config_source $darwin_config_target - end - set --append argv -I darwin-config=$darwin_config_source - case nixos-rebuild - set --local nixos_config_source (path resolve (status dirname)/system/$hostname.nix) - set --local nixos_config_target $user_nixpkgs/configuration.nix - if test ! -e $nixos_config_target - ln -s $nixos_config_source $nixos_config_target - end - if ! fish_is_root_user - set --append argv --use-remote-sudo - end - set --append argv -I nixos-config=$nixos_config_source - case home-manager - set --local hm_config_source (path resolve (status dirname)/user/$hostname.nix) - set --local hm_config_target $hm_config_dir/home.nix - if test ! -e $hm_config_target - ln -s $hm_config_source $hm_config_target - end - set --export HOME_MANAGER_CONFIG $hm_config_source -end - -set --function old_channels (path basename $channel_root/*) -set --function --export HOME_MANAGER_CONFIG (path resolve (status dirname)/user/$hostname.nix) - -for np in $NIX_PATH - echo $np | read --function --delimiter "=" channel new_src_rel - set --local new_src (path resolve $new_src_rel) - - if test $channel = darwin-config - continue - end - if set --local i (contains --index $channel $old_channels) - set --erase old_channels[$i] - end - set --local target $channel_root/$channel - set --local src (path resolve $target) - if test -e $src - if test $src = $new_src - # no need to re-link it - continue - else - unlink $target - end - end - ln -s $new_src $target -end - -for old in $channel_root/$old_channels - rm $old -end - -set --function cmd $current_script_name $argv - -if set --query IN_NIX_SHELL - eval $cmd -else - nix-shell --run "$cmd" -end diff --git a/nixos-rebuild b/nixos-rebuild deleted file mode 120000 index 2eaccdef..00000000 --- a/nixos-rebuild +++ /dev/null @@ -1 +0,0 @@ -home-manager \ No newline at end of file -- cgit 1.4.1