bodby

Nix shell for Factor

Published on .

Setting FACTOR_ROOTS in a Nix dev shell

Both snippets below assume you either only have extra/ or have all your vocabs in your project root (i.e. next to shell.nix or flake.nix). If you have multiple directories, each with vocabs inside, then list them all in FACTOR_ROOTS separated by a colon (semicolons on Windows, but gross blauegh and Nix doesn’t work there as of writing this anwyays).

Classic Nix

If you don’t use flakes:

mkShellNoCC {
  packages = [ factor-lang ];
  env.FACTOR_ROOTS = toString ./.;
}

Flakes

Since toString ./. properly evaluates to a store path when using Flakes, FACTOR_ROOTS won’t be set to the right directory and so changes made in your worktree won’t take effect if you try to factor -run=x.

Instead, write a shellHook that sets FACTOR_ROOTS:

mkShellNoCC {
  packages = [ factor-lang ];

  shellHook = ''
    CWD="$PWD"
    ROOT=""

    while [ "$CWD" != '/' ] && [ -z "$ROOT" ]; do
      if [ -e "$CWD/flake.nix" ]; then
        ROOT="$CWD"
      fi
      CWD="$(dirname "$CWD")"
    done

    if [ -n "$ROOT" ]; then
      export FACTOR_ROOTS="$ROOT"
    fi
  '';
}

Using $PWD directly won’t set it to the right value if you nix develop in a subdirectory.

nix · factor