comparison contrib/nix/flake.nix @ 50456:be676c313c7b

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`
author pacien <pacien.trangirard@pacien.net>
date Mon, 17 Apr 2023 00:27:46 +0200
parents
children
comparison
equal deleted inserted replaced
50455:59820ae507b1 50456:be676c313c7b
1 # flake.nix - Nix-defined package and devel env for the Mercurial project.
2 #
3 # Copyright 2021-2023 Pacien TRAN-GIRARD <pacien.trangirard@pacien.net>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 # Usage summary, from the root of this repository:
9 #
10 # Enter a shell with development tools:
11 # nix develop 'hg+file:.?dir=contrib/nix'
12 #
13 # Running mercurial:
14 # nix run 'hg+file:.?dir=contrib/nix' -- version
15 #
16 # Running the test suite in a sandbox:
17 # nix build 'hg+file:.?dir=contrib/nix#mercurial-tests' -L
18
19 {
20 inputs = {
21 nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
22 nixpkgs-black.url = "github:NixOS/nixpkgs/c7cb72b0"; # black 20.8b1
23 # rust-overlay.url = "github:oxalica/rust-overlay";
24 flake-utils.url = "github:numtide/flake-utils";
25 flaky-utils.url = "git+https://cgit.pacien.net/libs/flaky-utils";
26 };
27
28 outputs = {
29 self
30 , nixpkgs
31 , nixpkgs-black
32 # , rust-overlay
33 , flake-utils
34 , flaky-utils
35 }:
36 flake-utils.lib.eachDefaultSystem (system:
37 let
38 # overlays = [ (import rust-overlay) ];
39 pkgs = import nixpkgs { inherit system; };
40
41 # We're in the contrib/nix sub-directory.
42 src = ../..;
43
44 # For snapshots, to satisfy extension minimum version requirements.
45 dummyVersion = "99.99";
46
47 pin = {
48 # The test suite has issues with the latest/current versions of Python.
49 # Use an older recommended version instead, matching the CI.
50 python = pkgs.python39;
51
52 # The project uses a pinned version (rust/clippy.toml) for compiling,
53 # but uses formatter features from nightly.
54 # TODO: make cargo use the formatter from nightly automatically
55 # (not supported by rustup/cargo yet? workaround?)
56 # rustPlatform = pkgs.rust-bin.stable."1.61.0".default;
57 # rustPlatformFormatter = pkgs.rust-bin.nightly."2023-04-20".default;
58
59 # The CI uses an old version of the Black code formatter,
60 # itself depending on old Python libraries.
61 # The formatting rules have changed in more recent versions.
62 inherit (import nixpkgs-black { inherit system; }) black;
63 };
64
65 in rec {
66 apps.mercurial = apps.mercurial-rust;
67 apps.default = apps.mercurial;
68 apps.mercurial-c = flake-utils.lib.mkApp {
69 drv = packages.mercurial-c;
70 };
71 apps.mercurial-rust = flake-utils.lib.mkApp {
72 drv = packages.mercurial-rust;
73 };
74
75 packages.mercurial = packages.mercurial-rust;
76 packages.default = packages.mercurial;
77
78 packages.mercurial-c = pin.python.pkgs.buildPythonApplication {
79 format = "other";
80 pname = "mercurial";
81 version = "SNAPSHOT";
82 passthru.exePath = "/bin/hg";
83 inherit src;
84
85 postPatch = ''
86 echo 'version = b"${toString dummyVersion}"' \
87 > mercurial/__version__.py
88
89 patchShebangs .
90
91 for f in **/*.{py,c,t}; do
92 # not only used in shebangs
93 substituteAllInPlace "$f" '/bin/sh' '${pkgs.stdenv.shell}'
94 done
95 '';
96
97 buildInputs = with pin.python.pkgs; [
98 docutils
99 ];
100
101 nativeBuildInputs = with pkgs; [
102 gettext
103 installShellFiles
104 ];
105
106 makeFlags = [
107 "PREFIX=$(out)"
108 ];
109
110 buildPhase = ''
111 make local
112 '';
113
114 # Test suite is huge ; run on-demand in a separate package instead.
115 doCheck = false;
116 };
117
118 packages.mercurial-rust = packages.mercurial-c.overrideAttrs (super: {
119 cargoRoot = "rust";
120 cargoDeps = pkgs.rustPlatform.importCargoLock {
121 lockFile = "${src}/rust/Cargo.lock";
122 };
123
124 nativeBuildInputs = (super.nativeBuildInputs or []) ++ (
125 with pkgs.rustPlatform; [
126 cargoSetupHook
127 rust.cargo
128 rust.rustc
129 ]
130 );
131
132 makeFlags = (super.makeFlags or []) ++ [
133 "PURE=--rust"
134 ];
135 });
136
137 packages.mercurial-tests = pkgs.stdenv.mkDerivation {
138 pname = "mercurial-tests";
139 version = "SNAPSHOT";
140 inherit src;
141
142 buildInputs = with pkgs; [
143 pin.python
144 pin.black
145 unzip
146 which
147 sqlite
148 ];
149
150 postPatch = (packages.mercurial.postPatch or "") + ''
151 # * paths emitted by our wrapped hg look like ..hg-wrapped-wrapped
152 # * 'hg' is a wrapper; don't run using python directly
153 for f in **/*.t; do
154 substituteInPlace 2>/dev/null "$f" \
155 --replace '*/hg:' '*/*hg*:' \
156 --replace '"$PYTHON" "$BINDIR"/hg' '"$BINDIR"/hg'
157 done
158 '';
159
160 buildPhase = ''
161 export HGTEST_REAL_HG="${packages.mercurial}/bin/hg"
162 export HGMODULEPOLICY="rust+c"
163 export HGTESTFLAGS="--blacklist blacklists/nix"
164 make check 2>&1 | tee "$out"
165 '';
166 };
167
168 devShell = flaky-utils.lib.mkDevShell {
169 inherit pkgs;
170
171 tools = [
172 pin.python
173 pin.black
174 ];
175 };
176 });
177 }