Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-support: fix nix-prefetch-* on macOS #358685

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

jdelStrother
Copy link
Contributor

@jdelStrother jdelStrother commented Nov 24, 2024

Since nix 2.20, nix-store --add-fixed doesn't accept paths where the parent directory is a symlink. On macOS, /tmp is a symlink to /private/tmp, which causes a '/tmp' is a symlink error:

$ nix run github:nixos/nixpkgs/24.11-beta#nix-prefetch-git -- --url https://github.com/IFTTT/polo.git --rev 316aa2ac210a45a7fc400ab921831493d5dd21b8 --hash sha256
Initialized empty Git repository in /private/tmp/git-checkout-tmp-1Bf9bIv7/polo-316aa2a/.git/
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 51 (delta 8), reused 19 (delta 5), pack-reused 0 (from 0)
Unpacking objects: 100% (51/51), 19.57 KiB | 541.00 KiB/s, done.
From https://github.com/IFTTT/polo
 * branch            HEAD       -> FETCH_HEAD
Switched to a new branch 'fetchgit'
removing `.git'...
error: path '/tmp' is a symlink

Avoid this by resolving /tmp to a real directory in all the prefetch scripts

Fixes #358113


I don't see mention of the symlink-change in the nix 2.20 release notes, but I tested with this to narrow down the point where things started breaking:

nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/8b27c1239e5c421a2bbc2c65d52e4a6fbf2ff296.tar.gz \
  --packages 'nix-prefetch-git.override { nix = pkgs.nixVersions.nix_2_20; }' \
  --command 'nix-prefetch-git --url https://github.com/IFTTT/polo.git --rev 316aa2ac210a45a7fc400ab921831493d5dd21b8 --hash sha256'

Substitute nix_2_20 with nix_2_19 to see the old behavior which happily accepted symlinked directories.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

Copy link
Member

@emilazy emilazy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just a Nix bug. We can work around it, but I’d like to see a comment next to the occurrences pointing to the bug so we can remove it later when it’s fixed.

@@ -121,7 +121,7 @@ fi

sourceUrl="docker://$imageName@$imageDigest"

tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/skopeo-copy-tmp-XXXXXXXX")"
tmpPath="$(mktemp -d "$(realpath "${TMPDIR:-/tmp}/skopeo-copy-tmp-XXXXXXXX")")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Suggested change
tmpPath="$(mktemp -d "$(realpath "${TMPDIR:-/tmp}/skopeo-copy-tmp-XXXXXXXX")")"
tmpPath="$(realpath "$(mktemp -d --tmpdir skopeo-copy-tmp-XXXXXXXX)")"

and analoguously for all the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually used to use --tmpdir over 10 years ago, but was changed to use TMPDIR in 5e7a1cf.

It does look like my /usr/bin/mktemp does support --tmpdir on macOS 15.1.1, but I'm not sure how far back that goes. I could add coreutils to all the prefetch scripts to try & make sure we've always got a usable mktemp --tempdir ..?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I knew I kept my trusty 10.12 VM around for something:

Screenshot of macOS 10.12 confirming that its mktemp doesn’t support --tmpdir.

So this would be fine as‐is for 25.05 I think but not for 24.11. However, yes, we probably ought to just add coreutils to the list of common dependencies in pkgs/tools/package-management/nix-prefetch-scripts/default.nix; all our other Bash scripts assume GNU coreutils anyway.

@jdelStrother
Copy link
Contributor Author

I think this is just a Nix bug. We can work around it, but I’d like to see a comment next to the occurrences pointing to the bug so we can remove it later when it’s fixed.

OK, will try and get the nix devs to confirm whether it's a bug or feature so at least we have something concrete to link to.

Since nix 2.20, `nix-store --add-fixed` doesn't accept paths where the
parent directory is a symlink. On macOS, /tmp is a symlink to
/private/tmp, which causes a "'/tmp' is a symlink" error:

```
$ nix run github:nixos/nixpkgs/24.11-beta#nix-prefetch-git -- --url https://github.com/IFTTT/polo.git --rev 316aa2ac210a45a7fc400ab921831493d5dd21b8 --hash sha256
Initialized empty Git repository in /private/tmp/git-checkout-tmp-1Bf9bIv7/polo-316aa2a/.git/
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 51 (delta 8), reused 19 (delta 5), pack-reused 0 (from 0)
Unpacking objects: 100% (51/51), 19.57 KiB | 541.00 KiB/s, done.
From https://github.com/IFTTT/polo
 * branch            HEAD       -> FETCH_HEAD
Switched to a new branch 'fetchgit'
removing `.git'...
error: path '/tmp' is a symlink
```

Avoid this by resolving /tmp to a real directory in all the prefetch scripts
@jdelStrother jdelStrother force-pushed the prefetch-symlink-errors branch from b5af7fc to b81ee0e Compare November 25, 2024 09:38
macOS 10.12 doesn't have a usable --tmpdir flag on the builtin mktemp, but we can
make use of coreutil's mktemp instead.
@jdelStrother jdelStrother force-pushed the prefetch-symlink-errors branch from b81ee0e to 6cb8c5e Compare November 25, 2024 09:41
@jdelStrother jdelStrother requested a review from emilazy November 25, 2024 09:52
@jdelStrother
Copy link
Contributor Author

jdelStrother commented Nov 25, 2024

I think this is an intentional change rather than bug, judging by NixOS/nix@83c067c. Shall I just update the comments to point to that commit, rather than the issue at NixOS/nix#11941 ?

@emilazy
Copy link
Member

emilazy commented Nov 25, 2024

I think that rather that’s just an API change that they didn’t properly update all the callers of. I still think it is a Nix bug.

@NobbZ
Copy link
Contributor

NobbZ commented Nov 25, 2024

I agree with emilazy. This breaks something that previously worked. And adding something that is behind a symlink (but not necessarily the symlink itself!) seems a valid usecase to me.

@ofborg ofborg bot requested review from bennofs and offlinehacker November 26, 2024 02:16
@will
Copy link
Contributor

will commented Nov 29, 2024

I think this PR would fix what is currently breaking crystal2nix, but I haven't yet verified.

It starts doing its work, then gets error: path '/tmp' is a symlink when calling nix-prefetch-git , then fails to parse the empty stdout response as json

❯ crystal2nix
Initialized empty Git repository in /private/tmp/git-checkout-tmp-UCUG3iTb/backtracer.cr/.git/
From https://github.com/sija/backtracer.cr
 * tag               v1.2.2     -> FETCH_HEAD
Switched to a new branch 'fetchgit'
removing `.git'...
error: path '/tmp' is a symlink
Unhandled exception: Expected BeginObject but was EOF at line 1, column 1
  parsing Crystal2Nix::PrefetchJSON at line 0, column 0 (JSON::SerializableError)
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'raise<JSON::SerializableError>:NoReturn'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'Crystal2Nix::Worker#run:Nil'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in '__crystal_main'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'main'
Caused by: Expected BeginObject but was EOF at line 1, column 1 (JSON::ParseException)
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'raise<JSON::ParseException>:NoReturn'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'JSON::PullParser#raise<String>:NoReturn'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'Crystal2Nix::Worker#run:Nil'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in '__crystal_main'
  from /nix/store/fpnllrcmwbhpj2l3njin35jjb2f7jfdb-crystal2nix-0.3.0/bin/.crystal2nix-wrapped in 'main'

@jdelStrother
Copy link
Contributor Author

Yeah, sounds like the same issue.
I've re-written NixOS/nix#11941 to try & clarify it's not just some macOS weirdness.

@paparodeo paparodeo mentioned this pull request Dec 13, 2024
13 tasks
@Mic92 Mic92 added the backport release-24.11 Backport PR automatically label Dec 13, 2024
@Mic92 Mic92 dismissed emilazy’s stale review December 13, 2024 13:23

Requested comment was added.

@Mic92
Copy link
Member

Mic92 commented Dec 13, 2024

Tested those prefetchers and they seem to work. Will bring up the bug in Nix again in the next meeting.

@Mic92 Mic92 merged commit 3dc8ff1 into NixOS:master Dec 13, 2024
31 checks passed
@nix-backports
Copy link

nix-backports bot commented Dec 13, 2024

Successfully created backport PR for release-24.11:

@will
Copy link
Contributor

will commented Dec 13, 2024

Thanks!

@Mic92
Copy link
Member

Mic92 commented Dec 15, 2024

Work in progress pull request in nix that attempts to fix this: NixOS/nix#12046

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nix-prefetch-git seems broken on macos in release-24.11
5 participants