Skip to content

Commit

Permalink
Merge pull request #57 from nix-community/fix-macos-ci-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 authored Dec 25, 2020
2 parents a42379e + 6833ac2 commit fd48b56
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ $ echo "use nix" >> .envrc
$ direnv allow
```

## Experimental flakes support
## Flakes support

nix-direnv also comes with a flake alternative. The code is tested and works however
since future nix versions might change their api regarding this feature we cannot
guarantee stability after an nix upgrade.
Likewise `use_nix` the `use_flake` implementation will prevent garbage
collection of downloaded packages and also for flake inputs.

Save this file as `flake.nix`:

Expand Down
19 changes: 15 additions & 4 deletions direnvrc
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ _nix_add_gcroot() {
local storepath=$1
local symlink=$2

local stripped_pwd=${PWD/\//}
local stripped_pwd=${2/\//}
local escaped_pwd=${stripped_pwd//-/--}
local escaped_pwd=${escaped_pwd//\//-}
ln -fs "$storepath" "$symlink"
ln -fs "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
ln -fsn "$storepath" "$symlink"
ln -fsn "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
}

use_flake() {
watch_file flake.nix
watch_file flake.lock

local profile="$(direnv_layout_dir)/flake-profile"
local flake="$(direnv_layout_dir)/flake"
local profile_rc="${profile}.rc"

if [[ ! -e "$profile"
Expand All @@ -68,10 +69,20 @@ use_flake() {
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
[[ -d "$(direnv_layout_dir)" ]] || mkdir -p "$(direnv_layout_dir)"
local tmp_profile_rc=$(nix print-dev-env --profile "$tmp_profile")
drv=$(readlink "$tmp_profile")
# macos does not have realpath
if command -v realpath >/dev/null; then
drv=$(realpath "$tmp_profile")
else
drv=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$tmp_profile")
fi
echo "$tmp_profile_rc" > "$profile_rc"
rm -f "$tmp_profile" "$tmp_profile"*
_nix_add_gcroot "$drv" "$profile"

# also add garbage collection root for source
local flake_source=$(nix eval --impure --raw --expr 'builtins.getFlake (toString ./.)')
_nix_add_gcroot "$flake_source" "$flake"

log_status renewed cache
else
log_status using cached dev shell
Expand Down
7 changes: 5 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def support_flakes() -> bool:
"--json",
"--eval",
"--expr",
'(builtins.compareVersions "2.4" builtins.nixVersion) == 1',
"builtins ? getFlake",
]
proc = subprocess.run(cmd, text=True, capture_output=True, check=True)
return proc.stdout != "true"
return proc.stdout == "true"


class IntegrationTest(unittest.TestCase):
Expand Down Expand Up @@ -97,7 +97,10 @@ def test_nix_flake(self) -> None:
text=True,
)
sys.stderr.write(out2.stderr)
# check if gcroot symlink has been created and is still valid
self.assertTrue(self.testenv.joinpath(".direnv/flake").is_dir())
self.assertIn("using cached dev shell", out2.stderr)

self.assertEqual(out2.returncode, 0)

def tearDown(self) -> None:
Expand Down

0 comments on commit fd48b56

Please sign in to comment.