trofi / nix-guix-gentoo

Gentoo overlay for nix and guix functional package managers.
111 stars 9 forks source link

Inconsistency in creation of /tmp/nix-shell-{id} when using direnv #55

Open classic-ally opened 3 days ago

classic-ally commented 3 days ago

direnv has the ability to specify the use nix .envrc routine to autoload shell.nix environments when entering a folder that contains them through cd.

When entering a nix-shell environment in the typical way - nix-shell - a folder in /tmp is created:

allison@laptop ~ $ ls /tmp | grep nix
allison@laptop ~ $ nix-shell -p iftop

[nix-shell:~]$ ls /tmp | grep nix
nix-shell-48695-0

However, when entering a folder in which direnv loads the nix file, this does not occur:

allison@laptop ~/Downloads $ ls /tmp | grep nix
allison@laptop ~/Downloads $ cd dashboard
direnv: loading ~/Downloads/dashboard/.envrc
direnv: using nix
APP_STATE was not set. It has been set to 'development'.
direnv: export +APP_STATE +AR +AS +CC +CONFIG_SHELL +CXX +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_BUILD_TOP +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +NODE_ENV +NODE_PATH +OBJCOPY +OBJDUMP +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TEMPDIR +TMP +TMPDIR +__structuredAttrs +buildInputs +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +installPhase +mesonFlags +name +nativeBuildInputs +out +outputs +patches +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
allison@laptop ~/Downloads/dashboard $ ls /tmp | grep nix
allison@laptop ~/Downloads/dashboard $ 

The environment appears to otherwise be correctly loaded; I can do which npm in this nodeJS project and see it correctly references the nix store:

allison@laptop ~/Downloads/dashboard $ which npm
/nix/store/0xvgzkl74v6jrh79ibh7ss770dh4hb69-nodejs-22.10.0/bin/npm

npm can hence be ran. However, without the expected /tmp folder existing, there will be odd behaviour.

Nested nix-shells will not be able to be created, as they attempt to create their own folder under the shell's which does not exist:

allison@laptop ~/Downloads/dashboard $ nix-shell -p iftop
error: creating directory '/tmp/nix-shell-51184-0/nix-shell-53001-0': No such file or directory

Additionally, applications which attempt to write to the shell's tmp folder, will fail due to ENOENT:

allison@laptop ~/Downloads/dashboard $ npm run storybook

> dashboard@0.1.0 storybook
> storybook dev -p 6006 --ci

@storybook/core v8.4.5

(...)

SB_CORE-SERVER_0002 (CriticalPresetLoadError): Storybook failed to load the following preset: ./node_modules/@chromatic-com/storybook/dist/preset.js.

Please check whether your setup is correct, the Storybook dependencies (and their peer dependencies) are installed correctly and there are no package version clashes.

If you believe this is a bug, please open an issue on Github.

Error: ENOENT: no such file or directory, lstat '/tmp/nix-shell-51184-0'
    at Object.realpathSync (node:fs:2705:29)
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:296:9914
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:23:594
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:306:2506
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:23:594
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:542:1524
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:23:594
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:591:2909
    at ./node_modules/@chromatic-com/storybook/dist/preset.js:23:594
    at Object.<anonymous> (./node_modules/@chromatic-com/storybook/dist/preset.js:594:91)

More info: 

    at loadPreset (./node_modules/@storybook/core/dist/common/index.cjs:16477:13)

Running the exact same code in an environment instead created by nix-shell correctly spawns the temporary folder:

allison@laptop ~/Downloads/dashboard $ direnv block
direnv: error /home/allison/Downloads/dashboard/.envrc is blocked. Run `direnv allow` to approve its content
allison@laptop ~/Downloads/dashboard $ nix-shell
APP_STATE was not set. It has been set to 'development'.

[nix-shell:~/Downloads/dashboard]$ npm run storybook

> dashboard@0.1.0 storybook
> storybook dev -p 6006 --ci

@storybook/core v8.4.5

WARN The following packages are incompatible with Storybook 8.4.5 as they depend on different major versions of Storybook packages:
WARN - @storybook/addon-postcss@2.0.0
WARN 
WARN 
WARN Please consider updating your packages or contacting the maintainers for compatibility details.
WARN For more on Storybook 8 compatibility, see the linked GitHub issue:
WARN https://github.com/storybookjs/storybook/issues/26031
(node:54907) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
info => Serving static files from ././assets at /
info => Starting manager..
WARN No story files found for the specified pattern: components/**/*.mdx
info => Starting preview..
info Addon-docs: using MDX3
info => Using PostCSS preset with postcss@7.0.39
info => Using SWC as compiler
info => Using default Webpack5 setup
<i> [webpack-dev-middleware] wait until bundle finished
10% building 0/3 entries 3/8 dependencies 0/2 modulesinfo Using tsconfig paths for react-docgen
16% building 0/3 entries 361/451 dependencies 20/136 modules
warn - The safelist pattern `/^datatable-.*$/` doesn't match any Tailwind CSS classes.
warn - Fix this pattern or remove it from your `safelist` configuration.
warn - https://tailwindcss.com/docs/content-configuration#safelisting-classes
╭────────────────────────────────────────────────────╮
│                                                    │
│   Storybook 8.4.5 for nextjs started               │
│   239 ms for manager and 4.57 s for preview        │
│                                                    │
│    Local:            http://localhost:6006/        │
│    On your network:  http://100.99.253.25:6006/    │
│                                                    │
╰────────────────────────────────────────────────────╯

Notably, for comparison with how Nix handles this on other platforms, this installation using the Determinate Nix installer on Ubuntu 24.04 never creates a folder in /tmp representing the shell, or at least not one named with nix, irrespective of if direnv or nix-shell is used to invoke the environment:

allison@mycroharvest:~$ ls /tmp | grep nix
allison@mycroharvest:~$ cd dashboard/
direnv: error /home/allison/dashboard/.envrc is blocked. Run `direnv allow` to approve its content
allison@mycroharvest:~/dashboard$ nix-shell
APP_STATE was not set. It has been set to 'development'.

[nix-shell:~/dashboard]$ ls /tmp | grep nix

[nix-shell:~/dashboard]$ 
exit
allison@mycroharvest:~/dashboard$ direnv allow
direnv: loading ~/dashboard/.envrc
direnv: using nix
APP_STATE was not set. It has been set to 'development'.
direnv: export +APP_STATE +AR +AS +CC +CONFIG_SHELL +CXX +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_aarch64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_BUILD_TOP +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_aarch64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_STORE +NM +NODE_ENV +NODE_PATH +OBJCOPY +OBJDUMP +RANLIB +READELF +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +TEMP +TEMPDIR +TMP +TMPDIR +__structuredAttrs +buildInputs +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +installPhase +mesonFlags +name +nativeBuildInputs +out +outputs +patches +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH ~XDG_DATA_DIRS
allison@mycroharvest:~/dashboard$ ls /tmp | grep nix
allison@mycroharvest:~/dashboard$ 

This Gentoo installation is the only platform on which I have seen this issue; programs in the environment (and nested shells) work perfectly on the Ubuntu installation as well as on macOS.

I think perhaps this creation of a /tmp directory is nonstandard and thus causing issues?

trofi commented 3 days ago

Thank you for the report!

I never used direnv before. Will try something simple locally to see if it fails for me as well and will debug.