Skip to content

feat(nix): add flake with package, overlay and devShells - #104

Open
WangYiBen0 wants to merge 2 commits into
std-microblock:masterfrom
WangYiBen0:nix-flake
Open

WangYiBen0 wants to merge 2 commits into
std-microblock:masterfrom
WangYiBen0:nix-flake

Conversation

@WangYiBen0

Copy link
Copy Markdown

Summary

Adds a Nix flake: packages.celemod, packages.celemod-ui, overlays.default
and devShells.{default,ci} for x86_64-linux and aarch64-linux.

nix build .#celemod
nix run   .#celemod
nix develop
nix flake check

Verified

check result
nix eval .#packages.x86_64-linux.celemod.version 1.2.2, read from version.txt
nix build .#packages.x86_64-linux.celemod exit 0
$out/bin/ cele-mod (GTK wrapper) + .cele-mod-wrapped
nix flake check --all-systems --no-build exit 0

Implementation notes

Constraints of this nixpkgs or this repository, not preferences. Each is
commented at the relevant line and written up in nix/README.md.

  • Nightly Rust is required. src-tauri/src/lib.rs uses #![feature(try_blocks)], so a stable rustc fails with E0554. rustPlatform comes from rust-overlay's rust-bin.nightly.latest.default; the component .rustc ships no std sysroot (E0463).
  • cargo-tauri.hook drives the build. It runs tauri-build with the custom-protocol feature, so the frontend is embedded instead of loaded from build.devUrl at runtime. Building without it produces a binary that opens a window and shows nothing.
  • wrapGAppsHook4 supplies the GTK runtime environment (XDG_DATA_DIRS, GDK_PIXBUF_MODULE_PATH, GIO modules) and installs the desktop entry and icons.
  • cargoHash is top-level and cargoLock absent. buildRustPackage prefers cargoLock whenever it is set and silently ignores a top-level cargoHash, then fails with No hash was found while vendoring the git dependency game-scanner-1.2.0.
  • cargoRoot = ".". It also drives vendor staging, so any other value makes it look for Cargo.lock outside the workspace root; buildAndTestSubdir selects src-tauri.
  • CMAKE_POLICY_VERSION_MINIMUM = 3.5. prost-build 0.10.4 vendors protobuf and runs CMake on a pre-3.5 CMakeLists.txt.
  • rust-overlay is pinned by rev, not HEAD. A floating ref makes every evaluation call the GitHub API, which rate-limits.
  • nix/ must stay tracked. A git-repo flake cannot see untracked files.

Hashes

All three hashes in nix/versions.nix came from real fetch failures, never
guesses. scripts/update-nix-hashes.sh recomputes them, validates every
digest, asserts the written value matches the one just measured, and is
byte-identical on a second run.

Guard

checks.<system>.celemod-is-real fails the build if either package stops being
a real derivation, because nix flake check --no-build cannot tell a real
derivation from an empty runCommand stub. It asserts on nativeBuildInputs
rather than passthru.placeholder, which a plain runCommand never sets.

Scope

Desktop builds only. Android, AppImage/deb bundling and tauri build output
artifacts are untouched.


Adds a flake exposing packages.celemod, packages.celemod-ui,
overlays.default and devShells.{default,ci} for x86_64-linux and
aarch64-linux.

The package builds and runs:

  nix build .#celemod
  nix run   .#celemod

The Vite frontend is a separate derivation driven by fetchPnpmDeps. The Tauri
shell builds through cargo-tauri.hook, which runs tauri-build with the
custom-protocol feature so the finished dist/ is embedded; without it the
binary loads build.devUrl at runtime and shows an empty window.
wrapGAppsHook4 supplies the GTK runtime environment (XDG_DATA_DIRS,
GDK_PIXBUF_MODULE_PATH, GIO modules) and installs the desktop entry and icons.

Constraints worth knowing, each commented at the relevant line and written up
in nix/README.md:

  * nightly Rust is required (src-tauri/src/lib.rs uses #![feature(try_blocks)]);
    rustPlatform comes from rust-overlay's nightly .default toolchain
  * cargoHash must be top-level and cargoLock absent; supplying both makes
    buildRustPackage prefer importCargoLock, which then demands outputHashes
    for the git dependency
  * cargoRoot stays "." so fetchCargoVendor finds the workspace lockfile
  * rust-overlay is pinned by rev, not HEAD, so evaluation never calls the
    GitHub API
  * nix/ must stay git-tracked: a git-repo flake cannot see untracked files

Every hash in nix/versions.nix came from a real fetch failure.
scripts/update-nix-hashes.sh refreshes them idempotently and asserts that what
it writes matches what it measured.

checks.<system>.celemod-is-real guards against the packages collapsing to
runCommand placeholders, which `nix flake check --no-build` cannot detect.

@std-microblock std-microblock left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, the write-up is good and the traps you documented are real. A few concrete problems before merge:

Blocking

  1. nix/versions.nix says it can be reduced to { } and the flake still evaluates. It can't: nix/tools.nix unconditionally reads versions.version and versions.pnpm.depsHash, and mkCelemod's default cargoHash ? versions.cargoHash is evaluated too. Fix the comment or the code, otherwise following that instruction breaks the flake.
  2. scripts/update-nix-hashes.sh: --repo-root can never be reached, because the flake.nix existence check runs before the argument parsing. Move the while [ $# -gt 0 ] loop above it.
  3. nix/hooks/celemod.sh and nix/hooks/ci.sh are never sourced by either shellHook. Wire them in or drop them.
  4. The result symlink left by nix build is not in .gitignore. Please add /result (and /result-*).
  5. Nothing in CI runs the flake, while the three hashes have to be recomputed every time Cargo.lock or src/celemod-ui/pnpm-lock.yaml changes. Please add a check job (ubuntu-latest is enough), or say in nix/README.md that this is best effort and who refreshes the hashes.

Nits

  • cargoRoot is described three ways: the PR body says it is required, nix/README.md has a section titled "No cargoRoot", and nix/tools.nix defaults to ".". Please align.
  • nix/versions.nix hardcodes version = "1.2.2" while version.txt is the single source of truth. Drop it or explain why it is there.
  • nix/README.md says the refresher writes a fake hash into the file first; it actually builds a throwaway expression with pkgs.lib.fakeHash. Worth rewording.
  • nix/versions.nix has no trailing newline.

One question: is a Nix package meant to be supported long term here, and who owns the hash refresh when the lockfiles move? That answer decides how much of the above is blocking.

Blocking items:

  * nix/versions.nix claimed it could be reduced to `{ }`. It cannot:
    tools.nix reads cargoHash, pnpm.fetcherVersion and pnpm.depsHash
    unconditionally. The comment now says so.
  * Drop `version` from versions.nix entirely. It duplicated version.txt and
    tools.nix now reads version.txt through builtins.readFile, so the file
    holds hashes only.
  * scripts/update-nix-hashes.sh parsed arguments after validating REPO_ROOT,
    so --repo-root could never be reached. Parse first, then validate.
  * Sourcing nix/hooks/{celemod,ci}.sh from the shellHook instead of shipping
    them unused. ci.sh also drives colourless output, so CARGO_TERM_COLOR in
    mkShell now depends on the mode.
  * Ignore the /result symlinks nix build leaves behind.
  * Add .github/workflows/Nix.yml: evaluates the flake on every relevant
    change, builds the package, and fails when nix/versions.nix is stale
    relative to either lockfile.

Nits:

  * cargoRoot is documented as `cargoRoot = "."` everywhere; the README no
    longer contradicts tools.nix.
  * README describes the refresher accurately: it builds a throwaway
    expression with pkgs.lib.fakeHash rather than writing a fake into the file.
  * versions.nix ends with a newline.

Also renames the package attributes to match the upstream crate, whose
[package] and [[bin]] are both `cele-mod`.
@WangYiBen0

Copy link
Copy Markdown
Author

Thanks -- every point was correct. I reproduced each one against the source
before changing anything. All are addressed in 406c930.

Blocking

1. versions.nix could not be reduced to { }. Confirmed:
nix/tools.nix reads cargoHash, pnpm.fetcherVersion and pnpm.depsHash
unconditionally, so the comment was simply wrong. It now says the attributes
are required.

While fixing that I took the related nit: version is gone from
versions.nix. tools.nix reads version.txt through builtins.readFile, so
there is one source of truth rather than two that had to be kept in sync.

2. --repo-root was unreachable. Correct -- the flake.nix check ran
before the while [ $# -gt 0 ] loop. Arguments are now parsed first and the
check runs afterwards.

3. Hooks were dead files. Now sourced from shellHook: default sources
hooks/celemod.sh, ci sources hooks/ci.sh. Both are idempotent and return
success so nix develop --command is unaffected. ci.sh also drives
colourless output, which surfaced a second inconsistency: mkShell was
exporting CARGO_TERM_COLOR = "always" for both shells, overriding the hook,
so the ci shell was not actually colourless. That attribute is now
mode-dependent.

4. /result was not ignored. Added /result and /result-*.

5. CI did not run the flake. Added .github/workflows/Nix.yml, triggered
on changes to flake.nix, flake.lock, nix/**, the refresher, Cargo.lock
or pnpm-lock.yaml. It runs three steps:

  • nix flake check --all-systems --no-build -- cheap, offline, includes the
    cele-mod-is-real guard
  • nix build .#packages.x86_64-linux.cele-mod
  • ./scripts/update-nix-hashes.sh followed by git diff --exit-code, so a
    stale hash fails the job with a pointer to the fix

Nits

  • cargoRoot was described three ways. Now cargoRoot = "." everywhere;
    the README section is retitled and no longer says "no cargoRoot".
  • version hardcoded. Removed, as above.
  • README fakeHash wording. Now says the refresher builds a throwaway
    expression with pkgs.lib.fakeHash and reads the failure back.
  • Missing trailing newline. Fixed.

Naming

You are right that celemod vs cele-mod was confusing. Upstream's
src-tauri/Cargo.toml has [package] name = "cele-mod" and
[[bin]] name = "cele-mod", so the attribute and the executable are now the
same string: pkgs.cele-mod installs $out/bin/cele-mod. The frontend
attribute is cele-mod-ui.

On cele-mod-ui: it is the Vite build output as its own derivation,
$out/dist. It is not something you run -- the application derivation copies
that tree into src/celemod-ui/dist so tauri-build can embed it. It is
exposed because it is the cheapest target to build when iterating on the UI,
and because it makes the frontend build independently inspectable. That is now
documented in nix/lib.nix next to the attribute rather than left implicit.

Your question: long-term support and hash ownership

Being straight with you: I am not a maintainer here, and I would rather not
promise something I cannot deliver.

  • Hashes: scripts/update-nix-hashes.sh is self-service and now gated in
    CI -- if the lockfiles move and versions.nix is not refreshed, the Nix job
    fails on the diff with a message naming the script. So the work is
    detectable and mechanical rather than on one person's memory.
  • Long term: I am happy to fix breakage, but I cannot commit to being the
    sole owner of this package forever. If that is not enough, the honest options
    are to drop the package, or to list it as best-effort in nix/README.md with
    whoever adopts it taking the refresher with it. Your call which of those you
    prefer -- I will implement it either way.

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.

2 participants