Skip to content

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

Open
WangYiBen0 wants to merge 1 commit into
std-microblock:masterfrom
WangYiBen0:nix-flake
Open

WangYiBen0 wants to merge 1 commit 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.

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