Skip to content

Support a reproducible build via Nix#1

Merged
juliusikkala merged 1 commit intojuliusikkala:mainfrom
samestep:nix
Jul 16, 2025
Merged

Support a reproducible build via Nix#1
juliusikkala merged 1 commit intojuliusikkala:mainfrom
samestep:nix

Conversation

@samestep
Copy link
Copy Markdown
Contributor

Useful demo, thanks for making it!

This PR adds a Nix flake to allow building reproducibly in isolation.

No worries if you'd rather not merge this, I can still refer back to the PR regardless :)

@juliusikkala
Copy link
Copy Markdown
Owner

Interesting! Before merging this, I'd like to fix the issues with this repo with newer compiler versions. I managed to narrow it down to the Slang compiler sometimes dropping the upper 32 bits from pointers (again). After I've got that fixed, I'd like to try this Nix build and potentially merge it if it's good. I know next to nothing about Nix, but I have a friend who has hyped it up a lot over the years (pinging @jansol for opinions on this PR). Is there an easy way to test the Nix build without installing NixOS?

@samestep
Copy link
Copy Markdown
Contributor Author

Interesting! Before merging this, I'd like to fix the issues with this repo with newer compiler versions. I managed to narrow it down to the Slang compiler sometimes dropping the upper 32 bits from pointers (again).

Just to clarify, by "newer compiler versions" do you mean newer than the latest v2025.12.1 release, which is the current Slang version used in Nixpkgs (and used by the flake in this PR)?

If so, then I'll also point you to the discussion around NixOS/nixpkgs#424153 (comment) if you're interested; I did some delta debugging and determined that the specific change preventing the build in Nixpkgs from working on newer Slang commits is shader-slang/slang#7577. But of course, that's only an issue for using Nix.

After I've got that fixed, I'd like to try this Nix build and potentially merge it if it's good. I know next to nothing about Nix, but I have a friend who has hyped it up a lot over the years (pinging @jansol for opinions on this PR). Is there an easy way to test the Nix build without installing NixOS?

Yes, absolutely! My personal desktop machine is NixOS, but the machine I'm typing this on is Ubuntu with Nix installed, and I also have Nix installed on my MacBook. So if you're already on Linux or macOS, you can very easily install Nix via the official installer; or you could alternatively use the Determinate Nix installer which comes with flakes enabled by default. I've used both and they're both easy and work fine; I don't know much about the differences between them, except that the Determinate one is supposed to be easier to uninstall (can confirm that it's very easy to uninstall Determinate; but I've also never tried uninstalling from the official one).

Once you have Nix installed and flakes enabled, you can run the Nix build in this PR using the commands I wrote in README.md.

If you're interested in using Nix beyond that, I'd personally recommend Home Manager and direnv (both of which I use in my personal Nix setup in case you want to take a look at a more fleshed-out example setup), but I won't bore you with more details here unless you're interested.

Copy link
Copy Markdown

@jansol jansol left a comment

Choose a reason for hiding this comment

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

Looks fine.

Worth nothing, SDL2 is actually aliased to sdl2-compat in nixpkgs, but it runs fine anyway.

@@ -0,0 +1,50 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since there's a lockfile with pinned versions I'd prefer using a stable nixpkgs channel (say, nixos-25.04) so we get to use cached dependency builds for a longer time without updating the lockfile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh interesting, I wasn't aware that the cache on a release channel like nixos-25.05 lasts longer than the cache for nixpkgs-unstable. Out of curiosity, is there some documentation about the Nixpkgs cache policy that I can read?

Unfortunately switching this from nixpkgs-unstable to nixos-25.05 doesn't currently work, because the Slang version there is still only v2025.8.1, which is too old to compile this demo.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know of an official policy, the closest thing I know is https://nixos.wiki/wiki/Nix_channels

Anyway the idea is that stable gets fewer updates in general so it's more likely that a given build hash is still relevant.

inherit nativeBuildInputs buildInputs CPATH;
installPhase = ''
mkdir -p $out/bin
cp *.spv demo $out/bin/
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Having the .spv files in PATH if someone were to launch a (non-dev) shell with this or even install it globally... would get a bit awkward. I guess one option would be to wrap the demo to cd to a data dir in $out/usr/share before launching the actual binary? Alternatively changing the demo code so it explicitly looks there would be even nicer.

Anyway that's a fair bit of work for just a simple demo so that can also be done later if someone feels like it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair enough! This was just a minimal attempt with the goal of making no changes to the rest of the code. If I were making other changes then I would have also gotten rid of the pip install command that tries to run and times out in nix build, for instance.

Copy link
Copy Markdown
Contributor Author

@samestep samestep Jul 16, 2025

Choose a reason for hiding this comment

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

This demo also doesn't even work when running the built binary with cwd outside this repo clone, because it assumes that $PWD/data/Slang_LogoBug.png exists.

@juliusikkala
Copy link
Copy Markdown
Owner

juliusikkala commented Jul 16, 2025

Just to clarify, by "newer compiler versions" do you mean newer than the latest v2025.12.1 release, which is the current Slang version used in Nixpkgs (and used by the flake in this PR)?

Looks like v2025.12.1 should be fine, it seems that none of the problematic stuff has hit a release yet.

Yes, absolutely! My personal desktop machine is NixOS, but the machine I'm typing this on is Ubuntu with Nix installed, and I also have Nix installed on my MacBook. So if you're already on Linux or macOS, you can very easily install Nix via the official installer; or you could alternatively use the Determinate Nix installer which comes with flakes enabled by default. I've used both and they're both easy and work fine; I don't know much about the differences between them, except that the Determinate one is supposed to be easier to uninstall (can confirm that it's very easy to uninstall Determinate; but I've also never tried uninstalling from the official one).

Well, I just installed whatever nix version was in openSUSE's repos 🤠 Did a bit of googling on how to configure it to not want to create a random directory in root and enabling flakes etc.. Anyway, that somehow ended up creating a folder called ~ instead of going to the home folder, so I in my infinite wisdom typed rm -rf ~ and now my setup is completely borked. Might take a while until I'm back to a properly functioning system to test this again.

@samestep
Copy link
Copy Markdown
Contributor Author

samestep commented Jul 16, 2025

Oh god, that's awful! Never had that experience, sorry to have (indirectly) caused it for you :(

@juliusikkala
Copy link
Copy Markdown
Owner

Well, I mostly just lost configuration files, so it's a setback of a few hours. No need to feel too bad over it. Everything that is "irreplaceable" has a backup.

@juliusikkala
Copy link
Copy Markdown
Owner

I installed NixOS in a VM and managed to build and run this successfully, so I'm merging this!

@juliusikkala juliusikkala merged commit 82edaaf into juliusikkala:main Jul 16, 2025
@samestep samestep deleted the nix branch July 16, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants