diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..46f1751e --- /dev/null +++ b/.envrc @@ -0,0 +1,11 @@ +if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs=" +fi + +watch_file devenv.nix +watch_file devenv.lock +watch_file devenv.yaml +if ! use flake . --impure +then + echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2 +fi diff --git a/.gitignore b/.gitignore index ea8c4bf7..cd52e691 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /target + +# Devenv files, to handle the development shell defined by the flake +.devenv/ diff --git a/docs/maintainers-guide.md b/docs/maintainers-guide.md index cd31d69c..c9f493df 100644 --- a/docs/maintainers-guide.md +++ b/docs/maintainers-guide.md @@ -33,6 +33,7 @@ fastNLO tables to PineAPPL grids - `pineappl_py`: the crate that builds PineAPPL's Python interface - `xtask`: crate for [cargo-xtask] commands +- `.envrc`: [direnv] hooks (mainly enable Nix shell) - `.gitignore`: PineAPPL's Git ignore rules - `.readthedocs.yml`: configuration for PineAPPL's [Read-the-Docs] Python interface documentation @@ -44,6 +45,9 @@ - `README.md`: PineAPPL's repository README file. This file also serves as the homepage of [PineAPPL's website] - `_config.yml`: configuration file for PineAPPL's Github-pages website +- `flake.lock`: Nix [flake lock file] +- `flake.nix`: Nix [flake], which tracks cross-language dependencies to define a + reproducible build for the PineAPPL packages, and a suitable development shell - `install-capi.sh`: POSIX-compliant shell script to download and install PineAPPL's pre-built CAPI @@ -57,6 +61,9 @@ [lock file]: https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html [license v3]: https://www.gnu.org/licenses/gpl-3.0.en.html [PineAPPL's website]: https://nnpdf.github.io/pineappl/ +[flake]: https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-format +[flake lock file]: https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#lock-files +[direnv]: https://direnv.net/ ## Abbreviations and terminology diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..3be9fc15 --- /dev/null +++ b/flake.lock @@ -0,0 +1,330 @@ +{ + "nodes": { + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1713738183, + "narHash": "sha256-qd/MuLm7OfKQKyd4FAMqV4H6zYyOfef5lLzRrmXwKJM=", + "owner": "ipetkov", + "repo": "crane", + "rev": "f6c6a2fb1b8bd9b65d65ca9342dd0eb180a63f11", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "devenv": { + "inputs": { + "flake-compat": "flake-compat", + "nix": "nix", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks" + }, + "locked": { + "lastModified": 1691413946, + "narHash": "sha256-IQ9gWBnUMJaXXev9vjnUJDfX0y4tOprGF773xD0rwHA=", + "owner": "cachix", + "repo": "devenv", + "rev": "b4ce82b6a54022c688f9f916b9f7a9c9ed959de1", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1691475627, + "narHash": "sha256-aOgYmNqXyCHw1vaNDv5GqUAURiPMVej4xQNE37bCF8g=", + "owner": "nix-community", + "repo": "fenix", + "rev": "72b64bd857cacd2733ce99fbf0603bbe335d357c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "devenv", + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660459072, + "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "lowdown-src": { + "flake": false, + "locked": { + "lastModified": 1633514407, + "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", + "owner": "kristapsdz", + "repo": "lowdown", + "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", + "type": "github" + }, + "original": { + "owner": "kristapsdz", + "repo": "lowdown", + "type": "github" + } + }, + "nix": { + "inputs": { + "lowdown-src": "lowdown-src", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-regression": "nixpkgs-regression" + }, + "locked": { + "lastModified": 1676545802, + "narHash": "sha256-EK4rZ+Hd5hsvXnzSzk2ikhStJnD63odF7SzsQ8CuSPU=", + "owner": "domenkozar", + "repo": "nix", + "rev": "7c91803598ffbcfe4a55c44ac6d49b2cf07a527f", + "type": "github" + }, + "original": { + "owner": "domenkozar", + "ref": "relaxed-flakes", + "repo": "nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1678875422, + "narHash": "sha256-T3o6NcQPwXjxJMn2shz86Chch4ljXgZn746c2caGxd8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "126f49a01de5b7e35a43fd43f891ecf6d3a51459", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-regression": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1685801374, + "narHash": "sha256-otaSUoFEMM+LjBI1XL/xGB5ao6IwnZOXc47qhIgJe8U=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c37ca420157f4abc31e26f436c1145f8951ff373", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1713714899, + "narHash": "sha256-+z/XjO3QJs5rLE5UOf015gdVauVRQd2vZtsFkaXBq2Y=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6143fc5eeb9c4f00163267708e26191d1e918932", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1688056373, + "narHash": "sha256-2+SDlNRTKsgo3LBRiMUcoEUb6sDViRNQhzJquZ4koOI=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "5843cf069272d92b60c3ed9e55b7a8989c01d4c7", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "devenv": "devenv", + "fenix": "fenix", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1691410619, + "narHash": "sha256-npJ8zOZ/CQoZu+0kN5NU9iC5JbmbsucyK1DayPmHWPs=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "d9e9ca29814e95bb40db2216ed0973d9ba54b6a2", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..f5c9b1fb --- /dev/null +++ b/flake.nix @@ -0,0 +1,134 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + devenv.url = "github:cachix/devenv"; + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + fenix.url = "github:nix-community/fenix"; + fenix.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + crane, + devenv, + ... + } @ inputs: + flake-utils.lib.eachDefaultSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + in let + inherit (pkgs) lib; + craneLib = crane.lib.${system}; + + src = craneLib.cleanCargoSource (craneLib.path ./.); + # The following could be done automatically by Crane, but it will look for a + # top-level package name as well (not present in PineAPPL). Thus, doing it + # manually will save a Crane warning. + version = + (builtins.fromTOML (builtins.readFile (src + + "/Cargo.toml"))) + .workspace + .package + .version; + commonArgs = { + inherit src version; + strictDeps = false; + buildInputs = with pkgs; [lhapdf]; + nativeBuildInputs = with pkgs; [pkg-config]; + doCheck = false; + }; + + fileSetForCrate = crate: + lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./Cargo.toml + ./Cargo.lock + crate + # TODO: avoid passing all the folders + # (if strictly needed, at least avoid doing it explicitly) + ./pineappl + ./pineappl_applgrid + ./pineappl_capi + ./pineappl_cli + ./pineappl_fastnlo + ./pineappl_py + ./xtask + ]; + }; + + defaultName = "pineappl"; + cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {pname = defaultName;}); + cli = craneLib.buildPackage ( + commonArgs + // { + pname = defaultName; + cargoExtraArgs = "-p pineappl_cli"; + inherit cargoArtifacts; + src = fileSetForCrate ./pineappl_cli; + } + ); + # TODO: build with maturin + py = null; + in + { + packages = { + default = cli; + cli = cli; + pineappl-py = py; + }; + + apps.default = cli; + } + // { + devShells.default = let + pwd = builtins.getEnv "PWD"; + prefix = "${pwd}/target/prefix"; + lhapath = "${prefix}/share/LHAPDF"; + in + devenv.lib.mkShell { + inherit inputs pkgs; + modules = [ + ({config, ...}: { + packages = with pkgs; [ + maturin + (lhapdf.override { + python = + config.languages.python.package; + }) + ]; + + env = { + PREFIX = prefix; + LHAPDF_DATA_PATH = lhapath; + }; + enterShell = '' + # update path before entering the shell, when Nix packages updates + # already happened + export PATH=${prefix}/bin:$PATH + mkdir -p ${lhapath} + ''; + + languages.python = { + enable = true; + venv.enable = true; + }; + languages.rust = { + enable = true; + channel = "stable"; + }; + }) + ]; + }; + }); + + nixConfig = { + extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw="; + extra-substituters = "https://devenv.cachix.org"; + }; +}