aboutsummaryrefslogtreecommitdiff
path: root/scripts/nix-profile.sh.in
diff options
context:
space:
mode:
authorAlexander Bantyev <balsoft@balsoft.ru>2021-11-17 23:35:21 +0300
committerAlexander Bantyev <balsoft@balsoft.ru>2023-02-10 20:14:06 +0400
commit2384d360839e27edb3c928da858ec911415c8b4d (patch)
tree95e5360fc7016b9d171f4bad320486eda168bbb1 /scripts/nix-profile.sh.in
parenta31d7d4e5e5eeeb7ca12ca798dc383045e5be1a1 (diff)
A setting to follow XDG Base Directory standard
XDG Base Directory is a standard for locations for storing various files. Nix has a few files which seem to fit in the standard, but currently use a custom location directly in the user's ~, polluting it: - ~/.nix-profile - ~/.nix-defexpr - ~/.nix-channels This commit adds a config option (use-xdg-base-directories) to follow the XDG spec and instead use the following locations: - $XDG_STATE_HOME/nix/profile - $XDG_STATE_HOME/nix/defexpr - $XDG_STATE_HOME/nix/channels If $XDG_STATE_HOME is not set, it is assumed to be ~/.local/state. Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> Co-authored-by: Tim Fenney <kodekata@gmail.com> Co-authored-by: pasqui23 <pasqui23@users.noreply.github.com> Co-authored-by: Artturin <Artturin@artturin.com> Co-authored-by: John Ericson <Ericson2314@Yahoo.com>
Diffstat (limited to 'scripts/nix-profile.sh.in')
-rw-r--r--scripts/nix-profile.sh.in30
1 files changed, 27 insertions, 3 deletions
diff --git a/scripts/nix-profile.sh.in b/scripts/nix-profile.sh.in
index 5636085d4..264d9a8e2 100644
--- a/scripts/nix-profile.sh.in
+++ b/scripts/nix-profile.sh.in
@@ -2,11 +2,35 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
# Set up the per-user profile.
- NIX_LINK=$HOME/.nix-profile
+ NIX_LINK="$HOME/.nix-profile"
+ if [ -n "$XDG_STATE_HOME" ]; then
+ NIX_LINK_NEW="$XDG_STATE_HOME/nix/profile"
+ else
+ NIX_LINK_NEW="$HOME/.local/state/nix/profile"
+ fi
+ if ! [ -e "$NIX_LINK" ]; then
+ NIX_LINK="$NIX_LINK_NEW"
+ else
+ if [ -t 2 ] && [ -e "$NIX_LINK_NEW" ]; then
+ warning="\033[1;35mwarning:\033[0m"
+ printf "$warning Both %s and legacy %s exist; using the latter.\n" "$NIX_LINK_NEW" "$NIX_LINK" 1>&2
+ if [ "$(realpath "$NIX_LINK")" = "$(realpath "$NIX_LINK_NEW")" ]; then
+ printf " Since the profiles match, you can safely delete either of them.\n" 1>&2
+ else
+ # This should be an exceptionally rare occasion: the only way to get it would be to
+ # 1. Update to newer Nix;
+ # 2. Remove .nix-profile;
+ # 3. Set the $NIX_LINK_NEW to something other than the default user profile;
+ # 4. Roll back to older Nix.
+ # If someone did all that, they can probably figure out how to migrate the profile.
+ printf "$warning Profiles do not match. You should manually migrate from %s to %s.\n" "$NIX_LINK" "$NIX_LINK_NEW" 1>&2
+ fi
+ fi
+ fi
# Set up environment.
# This part should be kept in sync with nixpkgs:nixos/modules/programs/environment.nix
- export NIX_PROFILES="@localstatedir@/nix/profiles/default $HOME/.nix-profile"
+ export NIX_PROFILES="@localstatedir@/nix/profiles/default $NIX_LINK"
# Set $NIX_SSL_CERT_FILE so that Nixpkgs applications like curl work.
if [ -e /etc/ssl/certs/ca-certificates.crt ]; then # NixOS, Ubuntu, Debian, Gentoo, Arch
@@ -31,5 +55,5 @@ if [ -n "$HOME" ] && [ -n "$USER" ]; then
fi
export PATH="$NIX_LINK/bin:$PATH"
- unset NIX_LINK
+ unset NIX_LINK NIX_LINK_NEW
fi