Skip to content

Commit fd48b56

Browse files
authored
Merge pull request #57 from nix-community/fix-macos-ci-2
2 parents a42379e + 6833ac2 commit fd48b56

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ $ echo "use nix" >> .envrc
134134
$ direnv allow
135135
```
136136

137-
## Experimental flakes support
137+
## Flakes support
138138

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

143145
Save this file as `flake.nix`:
144146

direnvrc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ _nix_add_gcroot() {
4343
local storepath=$1
4444
local symlink=$2
4545

46-
local stripped_pwd=${PWD/\//}
46+
local stripped_pwd=${2/\//}
4747
local escaped_pwd=${stripped_pwd//-/--}
4848
local escaped_pwd=${escaped_pwd//\//-}
49-
ln -fs "$storepath" "$symlink"
50-
ln -fs "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
49+
ln -fsn "$storepath" "$symlink"
50+
ln -fsn "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd"
5151
}
5252

5353
use_flake() {
5454
watch_file flake.nix
5555
watch_file flake.lock
5656

5757
local profile="$(direnv_layout_dir)/flake-profile"
58+
local flake="$(direnv_layout_dir)/flake"
5859
local profile_rc="${profile}.rc"
5960

6061
if [[ ! -e "$profile"
@@ -68,10 +69,20 @@ use_flake() {
6869
local tmp_profile="$(direnv_layout_dir)/flake-profile.$$"
6970
[[ -d "$(direnv_layout_dir)" ]] || mkdir -p "$(direnv_layout_dir)"
7071
local tmp_profile_rc=$(nix print-dev-env --profile "$tmp_profile")
71-
drv=$(readlink "$tmp_profile")
72+
# macos does not have realpath
73+
if command -v realpath >/dev/null; then
74+
drv=$(realpath "$tmp_profile")
75+
else
76+
drv=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$tmp_profile")
77+
fi
7278
echo "$tmp_profile_rc" > "$profile_rc"
7379
rm -f "$tmp_profile" "$tmp_profile"*
7480
_nix_add_gcroot "$drv" "$profile"
81+
82+
# also add garbage collection root for source
83+
local flake_source=$(nix eval --impure --raw --expr 'builtins.getFlake (toString ./.)')
84+
_nix_add_gcroot "$flake_source" "$flake"
85+
7586
log_status renewed cache
7687
else
7788
log_status using cached dev shell

tests/test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def support_flakes() -> bool:
2424
"--json",
2525
"--eval",
2626
"--expr",
27-
'(builtins.compareVersions "2.4" builtins.nixVersion) == 1',
27+
"builtins ? getFlake",
2828
]
2929
proc = subprocess.run(cmd, text=True, capture_output=True, check=True)
30-
return proc.stdout != "true"
30+
return proc.stdout == "true"
3131

3232

3333
class IntegrationTest(unittest.TestCase):
@@ -97,7 +97,10 @@ def test_nix_flake(self) -> None:
9797
text=True,
9898
)
9999
sys.stderr.write(out2.stderr)
100+
# check if gcroot symlink has been created and is still valid
101+
self.assertTrue(self.testenv.joinpath(".direnv/flake").is_dir())
100102
self.assertIn("using cached dev shell", out2.stderr)
103+
101104
self.assertEqual(out2.returncode, 0)
102105

103106
def tearDown(self) -> None:

0 commit comments

Comments
 (0)