Skip to content

Support XDG-conformant paths for internal Nix files#146515

Draft
balsoft wants to merge 3 commits intoNixOS:stagingfrom
tweag:balsoft/nix-xdg
Draft

Support XDG-conformant paths for internal Nix files#146515
balsoft wants to merge 3 commits intoNixOS:stagingfrom
tweag:balsoft/nix-xdg

Conversation

@balsoft
Copy link
Member

@balsoft balsoft commented Nov 18, 2021

Ref: NixOS/nix#5588

This searches in $XDG_DATA_HOME/nix/profile everywhere where
previously we only looked in ~/.nix-profile, ditto for
.nix-channels and .nix-defexpr. This shouldn't break any
compatibility for now. It is highly recommended to use nixpkgs with
these changes if you want to use Nix with the XDG basedir PR, but not
doing so shouldn't result in any dramatic breakage -- in fact, NixOS
is perfectly usable without any user profile.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Ref: NixOS/nix#5588

This searches in $XDG_DATA_HOME/nix/profile everywhere where
previously we only looked in ~/.nix-profile. This shouldn't break
any compatibility for now. It is highly recommended to use nixpkgs
with these changes if you want to use Nix with the XDG basedir PR, but
not doing so shouldn't result in any dramatic breakage -- in fact,
NixOS is perfectly usable without any user profile.
@github-actions github-actions bot added 6.topic: emacs Text editor 6.topic: haskell General-purpose, statically typed, purely functional programming language 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: TeX Issues regarding texlive and TeX in general 6.topic: vim Advanced text editor 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 18, 2021
@ofborg ofborg bot requested review from cillianderoiste and cstrahan November 18, 2021 18:31
@ofborg ofborg bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Nov 18, 2021
Comment on lines +623 to +630
Copy link
Member

@Artturin Artturin Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic like this could be useful for setting the nix-profile so users could just use that env var in their scripts instead of having to accommodate 3 possible separate locations

NIX_USER_PROFILE_PATH perhaps?

on second thought that'll be nixos only but we'll want that on non nixos distros too so can nix somehow do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nix has a setup script that has to be sourced in order to bring nix into the PATH, with the proper ssl certs for packages to use. So that would be a good place to define the variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require an update to be simultaneous in both Nix and nixpkgs, which is a lot more difficult to orchestrate correctly.

Copy link
Member

@cab404 cab404 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM #zhfmsk

@SuperSandro2000
Copy link
Member

This needs to go to staging.


#140168 (comment)

I don't think we should squeeze in such a change last minute. There are probably more packages which need some adjustment and having that change a few months in unstable will probably reveal most of them. Especially DEs which heavily rely on XDG* shoulnd't break.

@balsoft balsoft changed the base branch from master to staging November 22, 2021 13:46

```
set debug-file-directory ~/.nix-profile/lib/debug
set debug-file-directory ~/.local/share/nix/profile/lib/debug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set debug-file-directory ~/.local/share/nix/profile/lib/debug
set debug-file-directory ~/.local/state/nix/profile/lib/debug

```

To install it into our environment, you can just run `nix-env -iA nixpkgs.myPackages`. If you want to load the packages to be built from a working copy of `nixpkgs` you just run `nix-env -f. -iA myPackages`. To explore what's been installed, just look through `~/.nix-profile/`. You can see that a lot of stuff has been installed. Some of this stuff is useful some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:
To install it into our environment, you can just run `nix-env -iA nixpkgs.myPackages`. If you want to load the packages to be built from a working copy of `nixpkgs` you just run `nix-env -f. -iA myPackages`. To explore what's been installed, just look through your user profile (`$XDG_DATA_HOME/nix/profile`, or a legacy location `~/.nix-profile/`). You can see that a lot of stuff has been installed. Some of this stuff is useful some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To install it into our environment, you can just run `nix-env -iA nixpkgs.myPackages`. If you want to load the packages to be built from a working copy of `nixpkgs` you just run `nix-env -f. -iA myPackages`. To explore what's been installed, just look through your user profile (`$XDG_DATA_HOME/nix/profile`, or a legacy location `~/.nix-profile/`). You can see that a lot of stuff has been installed. Some of this stuff is useful some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:
To install it into our environment, you can just run `nix-env -iA nixpkgs.myPackages`. If you want to load the packages to be built from a working copy of `nixpkgs` you just run `nix-env -f. -iA myPackages`. To explore what's been installed, just look through your user profile (`$XDG_STATE_HOME/nix/profile`, or a legacy location `~/.nix-profile/`). You can see that a lot of stuff has been installed. Some of this stuff is useful some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:

### Getting documentation {#sec-getting-documentation}

After building that new environment, look through `~/.nix-profile` to make sure everything is there that we wanted. Discerning readers will note that some files are missing. Look inside `~/.nix-profile/share/man/man1/` to verify this. There are no man pages for any of the Nix tools! This is because some packages like Nix have multiple outputs for things like documentation (see section 4). Let's make Nix install those as well.
After building that new environment, look through `$XDG_DATA_HOME/nix/profile` (or, as noted above, `~/.nix-profile`) to make sure everything is there that we wanted. Discerning readers will note that some files are missing. Look inside `$XDG_DATA_HOME/nix/profile/share/man/man1/` to verify this. There are no man pages for any of the Nix tools! This is because some packages like Nix have multiple outputs for things like documentation (see section 4). Let's make Nix install those as well.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
After building that new environment, look through `$XDG_DATA_HOME/nix/profile` (or, as noted above, `~/.nix-profile`) to make sure everything is there that we wanted. Discerning readers will note that some files are missing. Look inside `$XDG_DATA_HOME/nix/profile/share/man/man1/` to verify this. There are no man pages for any of the Nix tools! This is because some packages like Nix have multiple outputs for things like documentation (see section 4). Let's make Nix install those as well.
After building that new environment, look through `$XDG_STATE_HOME/nix/profile` (or, as noted above, `~/.nix-profile`) to make sure everything is there that we wanted. Discerning readers will note that some files are missing. Look inside `$XDG_STATE_HOME/nix/profile/share/man/man1/` to verify this. There are no man pages for any of the Nix tools! This is because some packages like Nix have multiple outputs for things like documentation (see section 4). Let's make Nix install those as well.

Comment on lines +267 to +269
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
export PATH=$XDG_DATA_HOME/nix/profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
export MANPATH=$XDG_DATA_HOME/nix/profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
export PATH=$XDG_DATA_HOME/nix/profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
export MANPATH=$XDG_DATA_HOME/nix/profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
XDG_DATA_HOME=${XDG_STATE_HOME:-$HOME/.local/share}
export PATH=$XDG_STATE_HOME/nix/profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
export MANPATH=$XDG_STATE_HOME/nix/profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be XDG_STATE_HOME=${XDG_STATE_HOME:-$HOME/.local/state}?

Comment on lines +300 to +302
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
if [ -d $XDG_DATA_HOME/nix/profile/etc/profile.d ]; then
for i in $XDG_DATA_HOME/nix/profile/etc/profile.d/*.sh; do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
if [ -d $XDG_DATA_HOME/nix/profile/etc/profile.d ]; then
for i in $XDG_DATA_HOME/nix/profile/etc/profile.d/*.sh; do
XDG_STATE_HOME=${XDG_STATE_HOME:-$HOME/.local/state}
if [ -d $XDG_STATE_HOME/nix/profile/etc/profile.d ]; then
for i in $XDG_STATE_HOME/nix/profile/etc/profile.d/*.sh; do

# Or if installed locally:
#
# source ~/.nix-profile/share/zsh-git-prompt/zshrc.sh
# source $XDG_DATA_HOME/nix/profile/share/zsh-git-prompt/zshrc.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# source $XDG_DATA_HOME/nix/profile/share/zsh-git-prompt/zshrc.sh
# source $XDG_STATE_HOME/nix/profile/share/zsh-git-prompt/zshrc.sh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work when XDG_STATE_HOME is not set which it isn't by default IIRC

+ DEFAULT_PATH += "/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user/";
+ DEFAULT_PATH += "/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/etc/profiles/per-user/:";
+ DEFAULT_PATH += pwd->pw_dir;
+ DEFAULT_PATH += "/.local/share/nix/bin:";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ DEFAULT_PATH += "/.local/share/nix/bin:";
+ DEFAULT_PATH += "/.local/state/nix/bin:";

Comment on lines +15 to +16
+ TEXMFNIXHOME = $XDG_DATA_HOME/nix/profile/share/texmf-nix
+ TEXMFNIXFALLBACK = ~/.local/share/nix/profile/share/texmf-nix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ TEXMFNIXHOME = $XDG_DATA_HOME/nix/profile/share/texmf-nix
+ TEXMFNIXFALLBACK = ~/.local/share/nix/profile/share/texmf-nix
+ TEXMFNIXHOME = $XDG_STATE_HOME/nix/profile/share/texmf-nix
+ TEXMFNIXFALLBACK = ~/.local/state/nix/profile/share/texmf-nix


impl Default for Format {
fn default() -> Self {
+ let data_home = std::env::var("XDG_DATA_HOME").unwrap_or("~/.local/share".to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ let data_home = std::env::var("XDG_DATA_HOME").unwrap_or("~/.local/share".to_string());
+ let data_home = std::env::var("XDG_STATE_HOME").unwrap_or("~/.local/state".to_string());

(add-to-list 'package-directory-list "/run/current-system/sw/share/emacs/site-lisp/elpa")

;; optional. use this if you install emacs packages to user profiles (with nix-env)
(add-to-list 'package-directory-list "~/.local/share/profile/nix/share/emacs/site-lisp/elpa")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(add-to-list 'package-directory-list "~/.local/share/profile/nix/share/emacs/site-lisp/elpa")
(add-to-list 'package-directory-list "~/.local/state/profile/nix/share/emacs/site-lisp/elpa")

@balsoft
Copy link
Member Author

balsoft commented Nov 23, 2021

@pasqui23 I disagree with moving those files to $XDG_STATE_HOME. As per the standard (emphasis mine):

The $XDG_STATE_HOME contains state data that should persist between (application) restarts, but that is not important or portable enough to the user that it should be stored in $XDG_DATA_HOME

The profile, defexpr and channels list are quite important for the user, unlike the examples given (the repl history, for example). Removing any one of them will break the experience in a non-subtle way: things will just stop working.

I would argue that this needs to be a separate PR in nix, which moves repl-history to the XDG_STATE_HOME.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/tweag-nix-dev-update-22/16251/1

@lovesegfault
Copy link
Member

@balsoft Can you rebase? Maybe we can get this landed :)

@balsoft
Copy link
Member Author

balsoft commented Apr 11, 2022

@lovesegfault I'd like to first merge the correspondent Nix PR

@SuperSandro2000 SuperSandro2000 added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 11, 2022
@Artturin Artturin added the 12.approvals: 1 This PR was reviewed and approved by one person. label May 4, 2022
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 2, 2022
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Feb 24, 2023
@Artturin
Copy link
Member

Artturin commented Mar 1, 2023

nix pr was merged 3 weeks ago

@SuperSandro2000
Copy link
Member

This PR blocks bumping nixVersions.stable to 2.14.

@infinisil infinisil mentioned this pull request May 22, 2023
12 tasks
@wegank wegank removed the 12.approvals: 1 This PR was reviewed and approved by one person. label Sep 7, 2023
@nyabinary
Copy link
Contributor

So is this PR stale?

@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 19, 2024
@wegank wegank marked this pull request as draft March 20, 2024 15:25
@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: emacs Text editor 6.topic: haskell General-purpose, statically typed, purely functional programming language 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: TeX Issues regarding texlive and TeX in general 6.topic: vim Advanced text editor 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.