Git workflow to avoid polluting a repo with nix files

I’m the only one in my team working on NixOS, so it’s hard to justify adding shell.nix and .envrc files to the repo.

I initially just kept them locally, and added them to my .git/info/exclude. But then:

  • they are not versioned so they don’t follow the repo history
  • I lost them once due to an unfortunate git clean

So I decided to add them to the repo anyway. It worked well but I had some understandable complaints.

Yesterday, I started experimenting with a git worktree based workflow:

  • I created a nix branch that contains my nix files, and a that created symlinks in the main worktree
  • I created a worktree with this nix branch in my repo and created the symlinks

Here is what the looks like:

# must be called in the main worktree.

# Find the root of the repo
ROOT=$(git rev-parse --show-toplevel)
cd "${ROOT}"

# Get the path to the "nix" worktree
NIX_WORKTREE=$(git worktree list | grep '\[nix\]' | cut -d' ' -f 1)

if [ ! -d "${NIX_WORKTREE}" ]; then
    echo "nix worktree not found" >2
    exit 1

# Create the links

# Root directory
ln -s "${NIX_WORKTREE}/root/shell.nix" "${ROOT}/shell.nix"
ln -s "${NIX_WORKTREE}/root/envrc"     "${ROOT}/.envrc"

# Keras example
ln -s "${NIX_WORKTREE}/keras_house_prices/shell.nix" "${ROOT}/python/client_examples/keras_house_prices/shell.nix"
ln -s "${NIX_WORKTREE}/keras_house_prices/envrc"     "${ROOT}/python/client_examples/keras_house_prices/.envrc"

# Ignore the .ignore, *.nix and .envrc files throughout the repo

cat <<EOF >> "${ROOT}/.git/info/exclude"
# Ignore nix files

The problem is that unlike a submodule, my nix worktree doesn’t really follows the history of the rest of the repo :frowning:

Is there a better workflow for this?

Why not just keep you nix files on a separate branch and then regualerly rebase this on top of master? That way you still have the files versioned but not everybody will get them.