diff --git a/dev/flake-module.nix b/dev/flake-module.nix index ab72d06..7f9da9c 100644 --- a/dev/flake-module.nix +++ b/dev/flake-module.nix @@ -1,80 +1,94 @@ -{ lib, inputs, withSystem, ... }: { +{ + lib, + inputs, + withSystem, + ... +}: +{ imports = [ inputs.pre-commit-hooks-nix.flakeModule inputs.hercules-ci-effects.flakeModule ]; - perSystem = { config, pkgs, ... }: { + perSystem = + { config, pkgs, ... }: + { - pre-commit.settings.hooks.nixfmt-rfc-style.enable = true; - # Temporarily disable rustfmt due to configuration issues - # pre-commit.settings.hooks.rustfmt.enable = true; - pre-commit.settings.settings.rust.cargoManifestPath = "./rust/Cargo.toml"; + pre-commit.settings.hooks.nixfmt-rfc-style.enable = true; + # Temporarily disable rustfmt due to configuration issues + # pre-commit.settings.hooks.rustfmt.enable = true; + pre-commit.settings.settings.rust.cargoManifestPath = "./rust/Cargo.toml"; - # Check that we're using ///-style doc comments in Rust code. - # - # Unfortunately, rustfmt won't do this for us yet - at least not - # without nightly, and it might do too much. - pre-commit.settings.hooks.rust-doc-comments = { - enable = true; - files = "\\.rs$"; - entry = "${pkgs.writeScript "rust-doc-comments" '' - #!${pkgs.runtimeShell} - set -uxo pipefail - grep -n -C3 --color=always -F '/**' "$@" - r=$? - set -e - if [ $r -eq 0 ]; then - echo "Please replace /**-style comments by /// style comments in Rust code." - exit 1 - fi - ''}"; + # Check that we're using ///-style doc comments in Rust code. + # + # Unfortunately, rustfmt won't do this for us yet - at least not + # without nightly, and it might do too much. + pre-commit.settings.hooks.rust-doc-comments = { + enable = true; + files = "\\.rs$"; + entry = "${pkgs.writeScript "rust-doc-comments" '' + #!${pkgs.runtimeShell} + set -uxo pipefail + grep -n -C3 --color=always -F '/**' "$@" + r=$? + set -e + if [ $r -eq 0 ]; then + echo "Please replace /**-style comments by /// style comments in Rust code." + exit 1 + fi + ''}"; + }; + + devShells.default = pkgs.mkShell { + name = "nix-bindings-devshell"; + strictDeps = true; + inputsFrom = [ config.nci.outputs.nix-bindings.devShell ]; + inherit (config.nci.outputs.nix-bindings.devShell.env) + LIBCLANG_PATH + NIX_CC_UNWRAPPED + ; + NIX_DEBUG_INFO_DIRS = + let + # TODO: add to Nixpkgs lib + getDebug = + pkg: + if pkg ? debug then + pkg.debug + else if pkg ? lib then + pkg.lib + else + pkg; + in + "${getDebug config.packages.nix}/lib/debug"; + buildInputs = [ + config.packages.nix + ]; + nativeBuildInputs = [ + pkgs.rust-analyzer + pkgs.nixfmt-rfc-style + pkgs.rustfmt + pkgs.pkg-config + pkgs.clang-tools # clangd + pkgs.valgrind + pkgs.gdb + pkgs.hci + # TODO: set up cargo-valgrind in shell and build + # currently both this and `cargo install cargo-valgrind` + # produce a binary that says ENOENT. + # pkgs.cargo-valgrind + ]; + shellHook = '' + ${config.pre-commit.installationScript} + source ${../rust/bindgen-gcc.sh} + echo 1>&2 "Welcome to the development shell!" + ''; + # rust-analyzer needs a NIX_PATH for some reason + NIX_PATH = "nixpkgs=${inputs.nixpkgs}"; + }; }; - - devShells.default = pkgs.mkShell { - name = "nix-bindings-devshell"; - strictDeps = true; - inputsFrom = [ config.nci.outputs.nix-bindings.devShell ]; - inherit (config.nci.outputs.nix-bindings.devShell.env) - LIBCLANG_PATH - NIX_CC_UNWRAPPED - ; - NIX_DEBUG_INFO_DIRS = - let - # TODO: add to Nixpkgs lib - getDebug = pkg: - if pkg?debug then pkg.debug - else if pkg?lib then pkg.lib - else pkg; - in - "${getDebug config.packages.nix}/lib/debug"; - buildInputs = [ - config.packages.nix - ]; - nativeBuildInputs = [ - pkgs.rust-analyzer - pkgs.nixfmt-rfc-style - pkgs.rustfmt - pkgs.pkg-config - pkgs.clang-tools # clangd - pkgs.valgrind - pkgs.gdb - pkgs.hci - # TODO: set up cargo-valgrind in shell and build - # currently both this and `cargo install cargo-valgrind` - # produce a binary that says ENOENT. - # pkgs.cargo-valgrind - ]; - shellHook = '' - ${config.pre-commit.installationScript} - source ${../rust/bindgen-gcc.sh} - echo 1>&2 "Welcome to the development shell!" - ''; - # rust-analyzer needs a NIX_PATH for some reason - NIX_PATH = "nixpkgs=${inputs.nixpkgs}"; + herculesCI = + hci@{ config, ... }: + { + ciSystems = [ "x86_64-linux" ]; }; - }; - herculesCI = hci@{ config, ... }: { - ciSystems = [ "x86_64-linux" ]; - }; flake = { }; } diff --git a/flake.nix b/flake.nix index f75342b..af537e3 100644 --- a/flake.nix +++ b/flake.nix @@ -10,19 +10,36 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; - outputs = inputs@{ self, flake-parts, ... }: - flake-parts.lib.mkFlake - { inherit inputs; } - ({ lib, ... }: { + outputs = + inputs@{ self, flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } ( + { + lib, + ... + }: + { imports = [ inputs.nix-cargo-integration.flakeModule inputs.flake-parts.flakeModules.partitions ./rust/nci.nix ]; - systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; - perSystem = { config, self', inputs', pkgs, ... }: { - packages.nix = inputs'.nix.packages.nix; - }; + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + perSystem = + { + config, + self', + inputs', + pkgs, + ... + }: + { + packages.nix = inputs'.nix.packages.nix; + }; partitionedAttrs.devShells = "dev"; partitionedAttrs.checks = "dev"; @@ -31,5 +48,6 @@ partitions.dev.module = { imports = [ ./dev/flake-module.nix ]; }; - }); + } + ); } diff --git a/rust/nci.nix b/rust/nci.nix index 87392b3..fabe122 100644 --- a/rust/nci.nix +++ b/rust/nci.nix @@ -1,69 +1,83 @@ { - perSystem = { lib, config, pkgs, ... }: { - # https://flake.parts/options/nix-cargo-integration - nci.projects.nix-bindings = { - path = ./.; - drvConfig = { - mkDerivation = { - buildInputs = [ - # stdbool.h - pkgs.stdenv.cc - ] ++ - (if config.packages.nix?libs - then - let l = config.packages.nix.libs; in [ - l.nix-expr-c - l.nix-store-c - l.nix-util-c - l.nix-flake-c + perSystem = + { + lib, + config, + pkgs, + ... + }: + { + # https://flake.parts/options/nix-cargo-integration + nci.projects.nix-bindings = { + path = ./.; + drvConfig = { + mkDerivation = { + buildInputs = [ + # stdbool.h + pkgs.stdenv.cc ] - else [ config.packages.nix ]); - nativeBuildInputs = [ - pkgs.pkg-config - ]; - # bindgen uses clang to generate bindings, but it doesn't know where to - # find our stdenv cc's headers, so when it's gcc, we need to tell it. - postConfigure = lib.optionalString pkgs.stdenv.cc.isGNU '' - source ${./bindgen-gcc.sh} - ''; - # Prepare the environment for Nix to work. - # Nix does not provide a suitable environment for running itself in - # the sandbox - not by default. We configure it to use a relocated store. - preCheck = '' - # nix needs a home directory - export HOME="$(mktemp -d $TMPDIR/home.XXXXXX)" + ++ ( + if config.packages.nix ? libs then + let + l = config.packages.nix.libs; + in + [ + l.nix-expr-c + l.nix-store-c + l.nix-util-c + l.nix-flake-c + ] + else + [ config.packages.nix ] + ); + nativeBuildInputs = [ + pkgs.pkg-config + ]; + # bindgen uses clang to generate bindings, but it doesn't know where to + # find our stdenv cc's headers, so when it's gcc, we need to tell it. + postConfigure = lib.optionalString pkgs.stdenv.cc.isGNU '' + source ${./bindgen-gcc.sh} + ''; + # Prepare the environment for Nix to work. + # Nix does not provide a suitable environment for running itself in + # the sandbox - not by default. We configure it to use a relocated store. + preCheck = '' + # nix needs a home directory + export HOME="$(mktemp -d $TMPDIR/home.XXXXXX)" - # configure a relocated store - store_data=$(mktemp -d $TMPDIR/store-data.XXXXXX) - export NIX_REMOTE="$store_data" - export NIX_BUILD_HOOK= - export NIX_CONF_DIR=$store_data/etc - export NIX_LOCALSTATE_DIR=$store_data/nix/var - export NIX_LOG_DIR=$store_data/nix/var/log/nix - export NIX_STATE_DIR=$store_data/nix/var/nix + # configure a relocated store + store_data=$(mktemp -d $TMPDIR/store-data.XXXXXX) + export NIX_REMOTE="$store_data" + export NIX_BUILD_HOOK= + export NIX_CONF_DIR=$store_data/etc + export NIX_LOCALSTATE_DIR=$store_data/nix/var + export NIX_LOG_DIR=$store_data/nix/var/log/nix + export NIX_STATE_DIR=$store_data/nix/var/nix - echo "Configuring relocated store at $NIX_REMOTE..." + echo "Configuring relocated store at $NIX_REMOTE..." - # Init ahead of time, because concurrent initialization is flaky - ${# Not using nativeBuildInputs because this should (hopefully) be - # the only place where we need a nix binary. Let's stay in control. - pkgs.buildPackages.nix}/bin/nix-store --init + # Init ahead of time, because concurrent initialization is flaky + ${ + # Not using nativeBuildInputs because this should (hopefully) be + # the only place where we need a nix binary. Let's stay in control. + pkgs.buildPackages.nix + }/bin/nix-store --init - echo "Store initialized." - ''; - }; - # NOTE: duplicated in flake.nix devShell - env = { - LIBCLANG_PATH = - lib.makeLibraryPath [ pkgs.buildPackages.llvmPackages.clang-unwrapped ]; - BINDGEN_EXTRA_CLANG_ARGS = - # Work around missing [[deprecated]] in clang - "-x c++ -std=c++2a"; - } // lib.optionalAttrs pkgs.stdenv.cc.isGNU { - # Avoid cc wrapper, because we only need to add the compiler/"system" dirs - NIX_CC_UNWRAPPED = "${pkgs.stdenv.cc.cc}/bin/gcc"; + echo "Store initialized." + ''; + }; + # NOTE: duplicated in flake.nix devShell + env = { + LIBCLANG_PATH = lib.makeLibraryPath [ pkgs.buildPackages.llvmPackages.clang-unwrapped ]; + BINDGEN_EXTRA_CLANG_ARGS = + # Work around missing [[deprecated]] in clang + "-x c++ -std=c++2a"; + } + // lib.optionalAttrs pkgs.stdenv.cc.isGNU { + # Avoid cc wrapper, because we only need to add the compiler/"system" dirs + NIX_CC_UNWRAPPED = "${pkgs.stdenv.cc.cc}/bin/gcc"; + }; }; }; }; - }; }