cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.56k stars 259 forks source link

An apostrophe in the project's path causes failure when using direnv activation #1173

Open zeorin opened 3 weeks ago

zeorin commented 3 weeks ago

Describe the bug An apostrophe in the project's path causes failure when using direnv activation.

To reproduce With direnv installed:

❯ mkdir foo\'s && cd foo\'s && devenv init
• Creating devenv.nix
• Creating devenv.yaml
• Creating .envrc
• Creating .gitignore
• Using Cachix: devenv
/home/zeorin/.cache/direnv/cas/77ef1c0690df0c18f8d629eb003689b7e6c35a13a4b70b258283f9622189d6fd:106:
export NIX_LDFLAGS
NIX_NO_SELF_RPATH=1
NIX_PKG_CONFIG_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu=1
export NIX_PKG_CONFIG_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu
NIX_STORE=/nix/store
export NIX_STORE
NM=nm
export NM
OBJCOPY=objcopy
export OBJCOPY
OBJDUMP=objdump
export OBJDUMP
OLDPWD=
export OLDPWD
OPTERR=1
OSTYPE=linux-gnu
PATH=/nix/store/44m0v10mi7wr6rfki271yg3lhv5cnwzj-hello/bin:/nix/store/y56vmnczakd9p0dsjl6jgnqrkqv04yxx-git-2.44.0/bin:/nix/store/fyxva0kkcmaigwk4218l0zdy8z3s9sj3-pkg-config-wrapper-0.29.2/bin:/nix/store/43n2dxi419b1jgv2bq1xg7y6n8k66xpb-patchelf-0.15.0/bin:/nix/store/ac1hb5dc2z4biwgy8mjrhlifffkkrvdq-gcc-wrapper-13.2.0/bin:/nix/store/a3mmr5jmrd0sjvmnc9vqvs388ppq1bnf-gcc-13.2.0/bin:/nix/store/fpv9yd0mwm1lyph02j9bdyxbjz8vaxzy-glibc-2.39-5-bin/bin:/nix/store/ifzwv2xqwdnv1gz87rxkizi67py5p3vj-coreutils-9.4/bin:/nix/store/jsjfmx7r6cpyixxsg7bjl5cy3y8hw7k7-binutils-wrapper-2.41/bin:/nix/store/hqvni28zpibl6jsqqimcvng6h6qm58xy-binutils-2.41/bin:/nix/store/ifzwv2xqwdnv1gz87rxkizi67py5p3vj-coreutils-9.4/bin:/nix/store/jg2i1qj5js5ckzvfq5b9p9j1gcv69750-findutils-4.9.0/bin:/nix/store/ksyif7fp4p0baafvdm5szd375s7qf6cz-diffutils-3.10/bin:/nix/store/237dff1igc3v09p9r23a37yw8dr04bv6-gnused-4.9/bin:/nix/store/avqi5nnx7qydr078ssgifc2hgzqipqgx-gnugrep-3.11/bin:/nix/store/2s1h0q7xhk33nsw90l0g53bxax0d19ih-gawk-5.2.2/bin:/nix/store/xwvgwr85gghbl66kj30sp19p2y1cypqv-gnutar-1.35/bin:/nix/store/vnfi66rm1dykn8gwwy5i4f48bhbk9x6x-gzip-1.13/bin:/nix/store/3bdjxil2cz8dkd6l5sf4qalla762m99d-bzip2-1.0.8-bin/bin:/nix/store/pg3wkw0as88676ib4ckbibrfwy5c5vzg-gnumake-4.4.1/bin:/nix/store/a1s263pmsci9zykm5xcdf7x9rv26w6d5-bash-5.2p26/bin:/nix/store/bf9rz7vvxannxp15wfi7x0bx5b68ky7z-patch-2.7.6/bin:/nix/store/jdg5lv2j7d765h8pn73qz72lilxrcb5x-xz-5.4.6-bin/bin:/nix/store/j3f0hag67n4piz2avqgf07gja8a2ifn6-file-5.45/bin
export PATH
PKG_CONFIG=pkg-config
export PKG_CONFIG
PS4=+: No such file or directory
/home/zeorin/.cache/direnv/cas/77ef1c0690df0c18f8d629eb003689b7e6c35a13a4b70b258283f9622189d6fd:153: pop_var_context: head of shell_variables not a function context
environment:1073: pop_var_context: head of shell_variables not a function context
./.envrc:3: pop_var_context: head of shell_variables not a function context
direnv: error exit status 127

Version

❯ devenv version
devenv 1.0.4 (x86_64-linux)
sandydoo commented 2 weeks ago

Could be related to https://github.com/cachix/devenv/issues/1107

zeorin commented 2 weeks ago

It still works fine with spaces for me.

sandydoo commented 1 week ago

The error you're seeing is coming from the bash rc script that's created by Nix. If you look through .direnv/flake-profile-<hash>.rc, you'll notice that the shell variables are wrapped in single quotes. The script will break in places where your project's directory shows up in those variables.

I would suggest opening an issue with either https://github.com/NixOS/nix or https://github.com/NixOS/nixpkgs. I can't quite tell whose responsibility it is to sanitise the paths.

A simple repo flake.nix that doesn't use direnv or devenv:

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }:
    let
      lib = nixpkgs.lib;
      forAllSystems = lib.genAttrs ["x86_64-linux" "x86_64-darwin" "aarch64-darwin" "aarch64-linux"];
    in
    {
      devShells = forAllSystems (system:
        let pkgs = nixpkgs.legacyPackages.${system};
        in {
          default = pkgs.mkShell {};
        }
      );
    };
}