From d4583afcd549bedbaba686e1be10172ec34d678f Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Mon, 6 Apr 2026 12:41:55 +0530 Subject: [PATCH] chore: fix nix build --- flake.nix | 22 +++++++++ nix/deps.nix | 55 +++++++++++++++++++--- nix/patches/nim-2.2.8-extra-mangling.patch | 48 +++++++++++++++++++ 3 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 nix/patches/nim-2.2.8-extra-mangling.patch diff --git a/flake.nix b/flake.nix index 6eaffdd..973745f 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,27 @@ forAllSystems = f: nixpkgs.lib.genAttrs stableSystems (system: f system); + # Pin Nim to 2.2.8 to match the project's minimum requirement (nim >= 2.2.6). + # nixpkgs ships Nim 2.2.4 whose nimble segfaults in sandboxed builds. + # The extra-mangling patch is rebased for the 2.2.8 source tree. + nimOverlay = final: prev: { + nim-unwrapped-2_2 = prev.nim-unwrapped-2_2.overrideAttrs (old: rec { + version = "2.2.8"; + src = prev.fetchurl { + url = "https://nim-lang.org/download/nim-${version}.tar.xz"; + hash = "sha256-EUGRr6CDxQWdy+XOiNvk9CVCz/BOLDAXZo7kOLwLjPw="; + }; + patches = builtins.filter (p: + !prev.lib.hasSuffix "extra-mangling-2.patch" (toString p) + ) old.patches ++ [ + ./nix/patches/nim-2.2.8-extra-mangling.patch + ]; + # Nim 2.2.8 has a cstring-to-string type error in excpt.nim when + # -d:nativeStacktrace is enabled. Drop it until upstream fixes it. + kochArgs = prev.lib.remove "-d:nativeStacktrace" old.kochArgs; + }); + }; + pkgsFor = forAllSystems ( system: import nixpkgs { inherit system; @@ -30,6 +51,7 @@ allowUnfree = true; }; overlays = [ + nimOverlay (final: prev: { androidEnvCustom = prev.callPackage ./nix/pkgs/android-sdk { }; androidPkgs = final.androidEnvCustom.pkgs; diff --git a/nix/deps.nix b/nix/deps.nix index b50b607..42eba71 100644 --- a/nix/deps.nix +++ b/nix/deps.nix @@ -7,19 +7,62 @@ stdenv.mkDerivation { inherit src; nativeBuildInputs = with pkgs; [ - jq rsync git nimble cacert moreutils + jq rsync git cacert moreutils ]; configurePhase = '' export XDG_CACHE_HOME=$TMPDIR export NIMBLE_DIR=$NIX_BUILD_TOP/nimbledir export HOME=$TMPDIR + git config --global user.email "nix@build" + git config --global user.name "Nix Build" + git config --global init.defaultBranch main ''; buildPhase = '' - nimble --version - nimble --silent --localdeps setup - nimble --silent --localdeps install -y --depsOnly + mkdir -p $NIMBLE_DIR/pkgs2 + + # Read nimble.lock and clone each package at the pinned revision. + # This bypasses nimble entirely, avoiding its segfault when it tries + # to build binary targets (e.g. testutils/ntu) in the Nix sandbox. + for pkg in $(jq -r '.packages | keys[]' nimble.lock); do + url=$(jq -r ".packages.\"$pkg\".url" nimble.lock) + rev=$(jq -r ".packages.\"$pkg\".vcsRevision" nimble.lock) + ver=$(jq -r ".packages.\"$pkg\".version" nimble.lock) + sha1=$(jq -r ".packages.\"$pkg\".checksums.sha1" nimble.lock) + + dest="$NIMBLE_DIR/pkgs2/$pkg-$ver-$sha1" + echo "Fetching $pkg@$ver ($rev) from $url" + git clone --quiet "$url" "$TMPDIR/clone_$pkg" + (cd "$TMPDIR/clone_$pkg" && git checkout --quiet "$rev") + + mkdir -p "$dest" + rsync -a --exclude='.git' "$TMPDIR/clone_$pkg/" "$dest/" + + # Create nimblemeta.json (nimble needs this to recognise installed packages) + files=$(cd "$dest" && find . -type f | sed 's|^\./|/|' | sort | jq -R . | jq -s .) + cat > "$dest/nimblemeta.json" < nimble.paths + for pkg in $NIMBLE_DIR/pkgs2/*/; do + echo "--path:\"$pkg\"" >> nimble.paths + done ''; installPhase = '' @@ -48,8 +91,8 @@ stdenv.mkDerivation { done ''; - # Make this a fixed-output derivation to allows internet access for Nimble. - outputHash = "sha256-OnirsXLj4HMVTbk+b4fcC+1K9MSMJyae6I7JO16WDno="; + # Make this a fixed-output derivation to allows internet access for git clones. + outputHash = "sha256-KTiUrarS6MmPksi3asJX7UQFLaNKB+a11RB6aHHPZgc="; outputHashAlgo = "sha256"; outputHashMode = "recursive"; } diff --git a/nix/patches/nim-2.2.8-extra-mangling.patch b/nix/patches/nim-2.2.8-extra-mangling.patch new file mode 100644 index 0000000..8547c3a --- /dev/null +++ b/nix/patches/nim-2.2.8-extra-mangling.patch @@ -0,0 +1,48 @@ +--- a/compiler/modulegraphs.nim ++++ b/compiler/modulegraphs.nim +@@ -506,7 +506,11 @@ + for i in 0..