Skip to content

Permission denied when creating .devenv in git worktrees with flakes #2003

Open
@apt-get-coke

Description

@apt-get-coke

Description

Devenv fails with permission errors when used in git worktrees with flakes enabled. This prevents developers from using git worktrees for parallel development workflows.

Problem

When using devenv in a git worktree with flakes, the source gets copied to the read-only Nix store, but devenv still tries to create its .devenv directory there, resulting in:

error: Permission denied (os error 13) at path "/nix/store/xxx-source/.devenv"
Error: IoError(Custom { kind: Other, error: "Failed to initialize task cache: Database error" })

Minimal Reproduction

  1. Create a temporary directory and cd into it
  2. Create devenv.nix:
{ pkgs, ... }:
{
  languages.python.enable = true;
  packages = [ pkgs.git ];
}
  1. Create flake.nix:
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    devenv.url = "github:cachix/devenv";
  };

  outputs = { self, nixpkgs, devenv, ... }@inputs:
    let
      systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
      forEachSystem = nixpkgs.lib.genAttrs systems;
    in {
      packages = forEachSystem (system: {
        devenv-up = self.devShells.${system}.default.config.procfileScript;
      });

      devShells = forEachSystem (system:
        let pkgs = nixpkgs.legacyPackages.${system};
        in {
          default = devenv.lib.mkShell {
            inherit inputs pkgs;
            modules = [ ./devenv.nix ];
          };
        });
    };
}
  1. Initialize git and create worktree:
git init
git add .
git commit -m "Initial commit"
git worktree add worktree-test -b test-branch
cd worktree-test
nix develop  # or devenv shell
  1. Observe the error:
error: Permission denied (os error 13) at path "/nix/store/xxx-source/.devenv"

Current Behavior

Devenv attempts to create .devenv in the source directory, which fails when the source is in the read-only Nix store.

Expected Behavior

Devenv should:

  1. Detect when running from a read-only source location
  2. Use an alternative location for .devenv (e.g., $XDG_CACHE_HOME/devenv/<project-hash>)
  3. Respect environment variables like DEVENV_ROOT or DEVENV_DOTFILE

Proposed Solutions

Option 1: Environment Variable Override

Allow users to override the .devenv location:

export DEVENV_DOTFILE="$HOME/.cache/devenv/my-project"

Option 2: Automatic Detection

Detect read-only sources and use cache directory automatically:

if source_is_readonly() {
    use_cache_directory();
}

Option 3: Configuration Option

Add configuration in devenv.nix:

{
  devenv.stateDirectory = "~/.cache/devenv/my-project";
}

Impact

This issue prevents effective use of git worktrees with devenv, impacting:

  • Parallel development workflows
  • CI/CD environments
  • Automated testing in isolated environments

Workaround

Currently, users must use complex wrapper scripts to work around this limitation. See our tracking issue for examples: https://github.com/exit0x/voxdia/issues/1002

Environment Information

  • OS: Linux 6.1.0-37-amd64
  • Architecture: x86_64
  • Nix version: nix (Nix) 2.29.0
  • Devenv version: devenv 1.6.1

Additional Context

This issue significantly impacts developer productivity when working on multiple features or reviewing PRs in parallel. Git worktrees are a standard workflow tool, and devenv's incompatibility with them in flake-enabled projects is a major limitation.

Related discussions:

  • voxdia#965 - Original issue discovery
  • voxdia#1001 - PR with initial workarounds
  • voxdia#1002 - Tracking issue for this problem

Would you consider any of the proposed solutions? I'm happy to contribute to the implementation if you can provide guidance on the preferred approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingflakesRelated to running devenv using the flake integration

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions