nix: add nix-defined package and devel env (flake)
authorpacien <pacien.trangirard@pacien.net>
Mon, 17 Apr 2023 00:27:46 +0200
changeset 50446 be676c313c7b
parent 50445 59820ae507b1
child 50447 392e2f31444a
nix: add nix-defined package and devel env (flake) This adds a Nix Flake which defines: - package recipies to build and run Mercurial (C, C+Rust variants), - a meta package to run the test suite in an isolated sandbox, - a development environment with some tools (notably Python and the Black formatter, pinned to match the CI). Python is pinned to the recommended version. The Rust toolchain is a fairly recent one provided by the Nixpkgs channel. It is not yet pinned to the same version as the "reference" Debian package, but this does not seem to cause any issue. Example usage of local commands are provided in the `flake.nix` file. Once merged, it should also be possible to pull and run directly from the source repository directly with something like: `nix run hg+https://foss.heptapod.net/mercurial/mercurial-devel?ref=TOPIC&dir=contrib/nix' -- version`
contrib/nix/flake.lock
contrib/nix/flake.nix
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/nix/flake.lock	Mon Apr 17 00:27:46 2023 +0200
@@ -0,0 +1,94 @@
+{
+  "nodes": {
+    "flake-utils": {
+      "inputs": {
+        "systems": "systems"
+      },
+      "locked": {
+        "lastModified": 1681202837,
+        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "flaky-utils": {
+      "locked": {
+        "lastModified": 1668472805,
+        "narHash": "sha256-hjRe8QFh2JMo9u6AaxQNGWfDWZxk3psULmPglqsjsLk=",
+        "ref": "refs/heads/master",
+        "rev": "c3f9daf4ec56276e040bc33e29c7eeaf1b99d91c",
+        "revCount": 33,
+        "type": "git",
+        "url": "https://cgit.pacien.net/libs/flaky-utils"
+      },
+      "original": {
+        "type": "git",
+        "url": "https://cgit.pacien.net/libs/flaky-utils"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1681482634,
+        "narHash": "sha256-cT/nr3L8khEYZSGp8qqwxFH+/q4/547MfyOdSj6MhBk=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "fda0d99c2cbbb5c89d8855d258cb0821bd9113ad",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixos-22.11",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "nixpkgs-black": {
+      "locked": {
+        "lastModified": 1605911135,
+        "narHash": "sha256-PoVe4Nu7UzYtOboytSzRY9sks6euoEzeCckBN+AIoTU=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "c7cb72b0cae397d311236d6773338efb4bd4f2d1",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "c7cb72b0",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "flake-utils": "flake-utils",
+        "flaky-utils": "flaky-utils",
+        "nixpkgs": "nixpkgs",
+        "nixpkgs-black": "nixpkgs-black"
+      }
+    },
+    "systems": {
+      "locked": {
+        "lastModified": 1681028828,
+        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
+        "owner": "nix-systems",
+        "repo": "default",
+        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-systems",
+        "repo": "default",
+        "type": "github"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/nix/flake.nix	Mon Apr 17 00:27:46 2023 +0200
@@ -0,0 +1,177 @@
+# flake.nix - Nix-defined package and devel env for the Mercurial project.
+#
+# Copyright 2021-2023 Pacien TRAN-GIRARD <pacien.trangirard@pacien.net>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+# Usage summary, from the root of this repository:
+#
+# Enter a shell with development tools:
+#   nix develop 'hg+file:.?dir=contrib/nix'
+#
+# Running mercurial:
+#   nix run 'hg+file:.?dir=contrib/nix' -- version
+#
+# Running the test suite in a sandbox:
+#   nix build 'hg+file:.?dir=contrib/nix#mercurial-tests' -L
+
+{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
+    nixpkgs-black.url = "github:NixOS/nixpkgs/c7cb72b0";  # black 20.8b1
+    # rust-overlay.url = "github:oxalica/rust-overlay";
+    flake-utils.url = "github:numtide/flake-utils";
+    flaky-utils.url = "git+https://cgit.pacien.net/libs/flaky-utils";
+  };
+
+  outputs = {
+    self
+  , nixpkgs
+  , nixpkgs-black
+  # , rust-overlay
+  , flake-utils
+  , flaky-utils
+  }:
+  flake-utils.lib.eachDefaultSystem (system:
+  let
+    # overlays = [ (import rust-overlay) ];
+    pkgs = import nixpkgs { inherit system; };
+
+    # We're in the contrib/nix sub-directory.
+    src = ../..;
+
+    # For snapshots, to satisfy extension minimum version requirements.
+    dummyVersion = "99.99";
+
+    pin = {
+      # The test suite has issues with the latest/current versions of Python.
+      # Use an older recommended version instead, matching the CI.
+      python = pkgs.python39;
+
+      # The project uses a pinned version (rust/clippy.toml) for compiling,
+      # but uses formatter features from nightly.
+      # TODO: make cargo use the formatter from nightly automatically
+      #       (not supported by rustup/cargo yet? workaround?)
+      # rustPlatform = pkgs.rust-bin.stable."1.61.0".default;
+      # rustPlatformFormatter = pkgs.rust-bin.nightly."2023-04-20".default;
+
+      # The CI uses an old version of the Black code formatter,
+      # itself depending on old Python libraries.
+      # The formatting rules have changed in more recent versions.
+      inherit (import nixpkgs-black { inherit system; }) black;
+    };
+
+  in rec {
+    apps.mercurial = apps.mercurial-rust;
+    apps.default = apps.mercurial;
+    apps.mercurial-c = flake-utils.lib.mkApp {
+      drv = packages.mercurial-c;
+    };
+    apps.mercurial-rust = flake-utils.lib.mkApp {
+      drv = packages.mercurial-rust;
+    };
+
+    packages.mercurial = packages.mercurial-rust;
+    packages.default = packages.mercurial;
+
+    packages.mercurial-c = pin.python.pkgs.buildPythonApplication {
+      format = "other";
+      pname = "mercurial";
+      version = "SNAPSHOT";
+      passthru.exePath = "/bin/hg";
+      inherit src;
+
+      postPatch = ''
+        echo 'version = b"${toString dummyVersion}"' \
+          > mercurial/__version__.py
+
+        patchShebangs .
+
+        for f in **/*.{py,c,t}; do
+          # not only used in shebangs
+          substituteAllInPlace "$f" '/bin/sh' '${pkgs.stdenv.shell}'
+        done
+      '';
+
+      buildInputs = with pin.python.pkgs; [
+        docutils
+      ];
+
+      nativeBuildInputs = with pkgs; [
+        gettext
+        installShellFiles
+      ];
+
+      makeFlags = [
+        "PREFIX=$(out)"
+      ];
+
+      buildPhase = ''
+        make local
+      '';
+
+      # Test suite is huge ; run on-demand in a separate package instead.
+      doCheck = false;
+    };
+
+    packages.mercurial-rust = packages.mercurial-c.overrideAttrs (super: {
+      cargoRoot = "rust";
+      cargoDeps = pkgs.rustPlatform.importCargoLock {
+        lockFile = "${src}/rust/Cargo.lock";
+      };
+
+      nativeBuildInputs = (super.nativeBuildInputs or []) ++ (
+        with pkgs.rustPlatform; [
+          cargoSetupHook
+          rust.cargo
+          rust.rustc
+        ]
+      );
+
+      makeFlags = (super.makeFlags or []) ++ [
+        "PURE=--rust"
+      ];
+    });
+
+    packages.mercurial-tests = pkgs.stdenv.mkDerivation {
+      pname = "mercurial-tests";
+      version = "SNAPSHOT";
+      inherit src;
+
+      buildInputs = with pkgs; [
+        pin.python
+        pin.black
+        unzip
+        which
+        sqlite
+      ];
+
+      postPatch = (packages.mercurial.postPatch or "") + ''
+        # * paths emitted by our wrapped hg look like ..hg-wrapped-wrapped
+        # * 'hg' is a wrapper; don't run using python directly
+        for f in **/*.t; do
+          substituteInPlace 2>/dev/null "$f" \
+            --replace '*/hg:' '*/*hg*:' \
+            --replace '"$PYTHON" "$BINDIR"/hg' '"$BINDIR"/hg'
+        done
+      '';
+
+      buildPhase = ''
+        export HGTEST_REAL_HG="${packages.mercurial}/bin/hg"
+        export HGMODULEPOLICY="rust+c"
+        export HGTESTFLAGS="--blacklist blacklists/nix"
+        make check 2>&1 | tee "$out"
+      '';
+    };
+
+    devShell = flaky-utils.lib.mkDevShell {
+      inherit pkgs;
+
+      tools = [
+        pin.python
+        pin.black
+      ];
+    };
+  });
+}