From 88b24310b5db181840fd108732c296cea10d8a2b Mon Sep 17 00:00:00 2001 From: Samuel Meenzen Date: Thu, 9 May 2024 15:20:48 +0200 Subject: [PATCH] chore: format nix using alejandra --- default.nix | 20 +-- engage.toml | 10 ++ flake.nix | 71 +++++------ nix/pkgs/book/default.nix | 36 +++--- nix/pkgs/default/cross-compilation-env.nix | 137 ++++++++++----------- nix/pkgs/default/default.nix | 120 +++++++++--------- nix/pkgs/oci-image/default.nix | 10 +- nix/shell.nix | 100 ++++++++------- 8 files changed, 264 insertions(+), 240 deletions(-) diff --git a/default.nix b/default.nix index f620865d..2b625713 100644 --- a/default.nix +++ b/default.nix @@ -1,10 +1,14 @@ -(import +( + import ( - let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in - fetchTarball { - url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; - sha256 = lock.nodes.flake-compat.locked.narHash; - } + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = lock.nodes.flake-compat.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } ) - { src = ./.; } -).defaultNix + {src = ./.;} +) +.defaultNix diff --git a/engage.toml b/engage.toml index 9dc2b31c..554c2ee7 100644 --- a/engage.toml +++ b/engage.toml @@ -35,6 +35,11 @@ group = "versions" name = "lychee" script = "lychee --version" +[[task]] +group = "versions" +name = "alejandra" +script = "alejandra --version" + [[task]] group = "lints" name = "cargo-fmt" @@ -66,6 +71,11 @@ group = "lints" name = "lychee" script = "lychee --offline docs" +[[task]] +group = "lints" +name = "alejandra" +script = "alejandra --check ." + [[task]] group = "tests" name = "cargo" diff --git a/flake.nix b/flake.nix index 91325443..dd11a7fa 100644 --- a/flake.nix +++ b/flake.nix @@ -19,10 +19,10 @@ attic.url = "github:zhaofengli/attic?ref=main"; }; - outputs = inputs: - let - # Keep sorted - mkScope = pkgs: pkgs.lib.makeScope pkgs.newScope (self: { + outputs = inputs: let + # Keep sorted + mkScope = pkgs: + pkgs.lib.makeScope pkgs.newScope (self: { craneLib = (inputs.crane.mkLib pkgs).overrideToolchain self.toolchain; @@ -34,24 +34,24 @@ book = self.callPackage ./nix/pkgs/book {}; - rocksdb = - let + rocksdb = let version = "9.1.1"; in - pkgs.rocksdb.overrideAttrs (old: { - inherit version; - src = pkgs.fetchFromGitHub { - owner = "facebook"; - repo = "rocksdb"; - rev = "v${version}"; - hash = "sha256-/Xf0bzNJPclH9IP80QNaABfhj4IAR5LycYET18VFCXc="; - }; - }); + pkgs.rocksdb.overrideAttrs (old: { + inherit version; + src = pkgs.fetchFromGitHub { + owner = "facebook"; + repo = "rocksdb"; + rev = "v${version}"; + hash = "sha256-/Xf0bzNJPclH9IP80QNaABfhj4IAR5LycYET18VFCXc="; + }; + }); shell = self.callPackage ./nix/shell.nix {}; # The Rust toolchain to use - toolchain = inputs + toolchain = + inputs .fenix .packages .${pkgs.pkgsBuildHost.system} @@ -62,23 +62,24 @@ sha256 = "sha256-opUgs6ckUQCyDxcB9Wy51pqhd0MPGHUVbwRKKPGiwZU="; }; }); - in - inputs.flake-utils.lib.eachDefaultSystem (system: - let + in + inputs.flake-utils.lib.eachDefaultSystem ( + system: let pkgs = inputs.nixpkgs.legacyPackages.${system}; - in - { - packages = { - default = (mkScope pkgs).default; - oci-image = (mkScope pkgs).oci-image; - book = (mkScope pkgs).book; - } - // - builtins.listToAttrs - (builtins.concatLists - (builtins.map - (crossSystem: - let + in { + packages = + { + default = (mkScope pkgs).default; + oci-image = (mkScope pkgs).oci-image; + book = (mkScope pkgs).book; + } + // builtins.listToAttrs + ( + builtins.concatLists + ( + builtins.map + ( + crossSystem: let binaryName = "static-${crossSystem}"; pkgsCrossStatic = (import inputs.nixpkgs { @@ -86,9 +87,9 @@ crossSystem = { config = crossSystem; }; - }).pkgsStatic; - in - [ + }) + .pkgsStatic; + in [ # An output for a statically-linked binary { name = binaryName; diff --git a/nix/pkgs/book/default.nix b/nix/pkgs/book/default.nix index cc0464d6..692a0ea1 100644 --- a/nix/pkgs/book/default.nix +++ b/nix/pkgs/book/default.nix @@ -1,27 +1,29 @@ # Keep sorted -{ default -, inputs -, mdbook -, stdenv +{ + default, + inputs, + mdbook, + stdenv, }: - stdenv.mkDerivation { pname = "${default.pname}-book"; version = default.version; + src = let + filter = inputs.nix-filter.lib; + in + filter { + root = inputs.self; - src = let filter = inputs.nix-filter.lib; in filter { - root = inputs.self; - - # Keep sorted - include = [ - "book.toml" - "conduit-example.toml" - "debian/README.md" - "docs" - "README.md" - ]; - }; + # Keep sorted + include = [ + "book.toml" + "conduit-example.toml" + "debian/README.md" + "docs" + "README.md" + ]; + }; nativeBuildInputs = [ mdbook diff --git a/nix/pkgs/default/cross-compilation-env.nix b/nix/pkgs/default/cross-compilation-env.nix index fac85e02..af669e46 100644 --- a/nix/pkgs/default/cross-compilation-env.nix +++ b/nix/pkgs/default/cross-compilation-env.nix @@ -1,100 +1,93 @@ -{ lib -, pkgsBuildHost -, rust -, stdenv +{ + lib, + pkgsBuildHost, + rust, + stdenv, }: - lib.optionalAttrs stdenv.hostPlatform.isStatic { ROCKSDB_STATIC = ""; } -// -{ +// { CARGO_BUILD_RUSTFLAGS = lib.concatStringsSep - " " - ([] - # This disables PIE for static builds, which isn't great in terms of - # security. Unfortunately, my hand is forced because nixpkgs' - # `libstdc++.a` is built without `-fPIE`, which precludes us from - # leaving PIE enabled. - ++ lib.optionals - stdenv.hostPlatform.isStatic - [ "-C" "relocation-model=static" ] - ++ lib.optionals - (stdenv.buildPlatform.config != stdenv.hostPlatform.config) - [ "-l" "c" ] - ++ lib.optionals - # This check has to match the one [here][0]. We only need to set - # these flags when using a different linker. Don't ask me why, though, - # because I don't know. All I know is it breaks otherwise. - # - # [0]: https://github.com/NixOS/nixpkgs/blob/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L37-L40 - ( - # Nixpkgs doesn't check for x86_64 here but we do, because I - # observed a failure building statically for x86_64 without - # including it here. Linkers are weird. - (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64) - && stdenv.hostPlatform.isStatic - && !stdenv.isDarwin - && !stdenv.cc.bintools.isLLVM - ) - [ - "-l" - "stdc++" - "-L" - "${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib" - ] - ); + " " + ( + [] + # This disables PIE for static builds, which isn't great in terms of + # security. Unfortunately, my hand is forced because nixpkgs' + # `libstdc++.a` is built without `-fPIE`, which precludes us from + # leaving PIE enabled. + ++ lib.optionals + stdenv.hostPlatform.isStatic + ["-C" "relocation-model=static"] + ++ lib.optionals + (stdenv.buildPlatform.config != stdenv.hostPlatform.config) + ["-l" "c"] + ++ lib.optionals + # This check has to match the one [here][0]. We only need to set + # these flags when using a different linker. Don't ask me why, though, + # because I don't know. All I know is it breaks otherwise. + # + # [0]: https://github.com/NixOS/nixpkgs/blob/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L37-L40 + ( + # Nixpkgs doesn't check for x86_64 here but we do, because I + # observed a failure building statically for x86_64 without + # including it here. Linkers are weird. + (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isx86_64) + && stdenv.hostPlatform.isStatic + && !stdenv.isDarwin + && !stdenv.cc.bintools.isLLVM + ) + [ + "-l" + "stdc++" + "-L" + "${stdenv.cc.cc.lib}/${stdenv.hostPlatform.config}/lib" + ] + ); } - # What follows is stolen from [here][0]. Its purpose is to properly configure # compilers and linkers for various stages of the build, and even covers the # case of build scripts that need native code compiled and run on the build # platform (I think). # # [0]: https://github.com/NixOS/nixpkgs/blob/5cdb38bb16c6d0a38779db14fcc766bc1b2394d6/pkgs/build-support/rust/lib/default.nix#L57-L80 -// -( +// ( let inherit (rust.lib) envVars; in - lib.optionalAttrs + lib.optionalAttrs (stdenv.targetPlatform.rust.rustcTarget != stdenv.hostPlatform.rust.rustcTarget) ( let inherit (stdenv.targetPlatform.rust) cargoEnvVarTarget; - in - { + in { "CC_${cargoEnvVarTarget}" = envVars.ccForTarget; "CXX_${cargoEnvVarTarget}" = envVars.cxxForTarget; "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForTarget; } ) - // - ( - let - inherit (stdenv.hostPlatform.rust) cargoEnvVarTarget rustcTarget; - in - { - "CC_${cargoEnvVarTarget}" = envVars.ccForHost; - "CXX_${cargoEnvVarTarget}" = envVars.cxxForHost; - "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForHost; - CARGO_BUILD_TARGET = rustcTarget; - } - ) - // - ( - let - inherit (stdenv.buildPlatform.rust) cargoEnvVarTarget; - in - { - "CC_${cargoEnvVarTarget}" = envVars.ccForBuild; - "CXX_${cargoEnvVarTarget}" = envVars.cxxForBuild; - "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForBuild; - HOST_CC = "${pkgsBuildHost.stdenv.cc}/bin/cc"; - HOST_CXX = "${pkgsBuildHost.stdenv.cc}/bin/c++"; - } - ) + // ( + let + inherit (stdenv.hostPlatform.rust) cargoEnvVarTarget rustcTarget; + in { + "CC_${cargoEnvVarTarget}" = envVars.ccForHost; + "CXX_${cargoEnvVarTarget}" = envVars.cxxForHost; + "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForHost; + CARGO_BUILD_TARGET = rustcTarget; + } + ) + // ( + let + inherit (stdenv.buildPlatform.rust) cargoEnvVarTarget; + in { + "CC_${cargoEnvVarTarget}" = envVars.ccForBuild; + "CXX_${cargoEnvVarTarget}" = envVars.cxxForBuild; + "CARGO_TARGET_${cargoEnvVarTarget}_LINKER" = envVars.linkerForBuild; + HOST_CC = "${pkgsBuildHost.stdenv.cc}/bin/cc"; + HOST_CXX = "${pkgsBuildHost.stdenv.cc}/bin/c++"; + } + ) ) diff --git a/nix/pkgs/default/default.nix b/nix/pkgs/default/default.nix index 4577fea9..a89ae7b8 100644 --- a/nix/pkgs/default/default.nix +++ b/nix/pkgs/default/default.nix @@ -1,43 +1,42 @@ # Dependencies (keep sorted) -{ craneLib -, inputs -, lib -, pkgsBuildHost -, rocksdb -, rust -, stdenv - -# Options (keep sorted) -, default-features ? true -, features ? [] -, profile ? "release" -}: - -let - buildDepsOnlyEnv = - let - rocksdb' = rocksdb.override { - enableJemalloc = builtins.elem "jemalloc" features; - }; - in +{ + craneLib, + inputs, + lib, + pkgsBuildHost, + rocksdb, + rust, + stdenv, + # Options (keep sorted) + default-features ? true, + features ? [], + profile ? "release", +}: let + buildDepsOnlyEnv = let + rocksdb' = rocksdb.override { + enableJemalloc = builtins.elem "jemalloc" features; + }; + in { NIX_OUTPATH_USED_AS_RANDOM_SEED = "randomseed"; # https://crane.dev/faq/rebuilds-bindgen.html ROCKSDB_INCLUDE_DIR = "${rocksdb'}/include"; ROCKSDB_LIB_DIR = "${rocksdb'}/lib"; } - // - (import ./cross-compilation-env.nix { + // (import ./cross-compilation-env.nix { # Keep sorted inherit lib pkgsBuildHost rust - stdenv; + stdenv + ; }); - buildPackageEnv = { - CONDUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev; - } // buildDepsOnlyEnv; + buildPackageEnv = + { + CONDUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev; + } + // buildDepsOnlyEnv; commonAttrs = { inherit @@ -45,18 +44,22 @@ let cargoToml = "${inputs.self}/Cargo.toml"; }) pname - version; + version + ; - src = let filter = inputs.nix-filter.lib; in filter { - root = inputs.self; + src = let + filter = inputs.nix-filter.lib; + in + filter { + root = inputs.self; - # Keep sorted - include = [ - "Cargo.lock" - "Cargo.toml" - "src" - ]; - }; + # Keep sorted + include = [ + "Cargo.lock" + "Cargo.toml" + "src" + ]; + }; nativeBuildInputs = [ # bindgen needs the build platform's libclang. Apparently due to "splicing @@ -68,28 +71,31 @@ let CARGO_PROFILE = profile; }; in + craneLib.buildPackage (commonAttrs + // { + cargoArtifacts = craneLib.buildDepsOnly (commonAttrs + // { + env = buildDepsOnlyEnv; + }); -craneLib.buildPackage ( commonAttrs // { - cargoArtifacts = craneLib.buildDepsOnly (commonAttrs // { - env = buildDepsOnlyEnv; - }); + cargoExtraArgs = + "--locked " + + lib.optionalString + (!default-features) + "--no-default-features " + + lib.optionalString + (features != []) + "--features " + + (builtins.concatStringsSep "," features); - cargoExtraArgs = "--locked " - + lib.optionalString - (!default-features) - "--no-default-features " - + lib.optionalString - (features != []) - "--features " + (builtins.concatStringsSep "," features); + # This is redundant with CI + doCheck = false; - # This is redundant with CI - doCheck = false; + env = buildPackageEnv; - env = buildPackageEnv; + passthru = { + env = buildPackageEnv; + }; - passthru = { - env = buildPackageEnv; - }; - - meta.mainProgram = commonAttrs.pname; -}) + meta.mainProgram = commonAttrs.pname; + }) diff --git a/nix/pkgs/oci-image/default.nix b/nix/pkgs/oci-image/default.nix index 8b359ce4..1e2475c2 100644 --- a/nix/pkgs/oci-image/default.nix +++ b/nix/pkgs/oci-image/default.nix @@ -1,10 +1,10 @@ # Keep sorted -{ default -, dockerTools -, lib -, tini +{ + default, + dockerTools, + lib, + tini, }: - dockerTools.buildImage { name = default.pname; tag = "next"; diff --git a/nix/shell.nix b/nix/shell.nix index bd070fe6..611f1776 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -1,61 +1,69 @@ # Keep sorted -{ cargo-deb -, default -, engage -, go -, inputs -, jq -, lychee -, mdbook -, mkShell -, olm -, system -, taplo -, toolchain +{ + alejandra, + cargo-deb, + default, + engage, + go, + inputs, + jq, + lychee, + mdbook, + mkShell, + olm, + system, + taplo, + toolchain, }: - mkShell { - env = default.env // { - # Rust Analyzer needs to be able to find the path to default crate - # sources, and it can read this environment variable to do so. The - # `rust-src` component is required in order for this to work. - RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library"; - }; + env = + default.env + // { + # Rust Analyzer needs to be able to find the path to default crate + # sources, and it can read this environment variable to do so. The + # `rust-src` component is required in order for this to work. + RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library"; + }; # Development tools - nativeBuildInputs = default.nativeBuildInputs ++ [ - # Always use nightly rustfmt because most of its options are unstable - # - # This needs to come before `toolchain` in this list, otherwise - # `$PATH` will have stable rustfmt instead. - inputs.fenix.packages.${system}.latest.rustfmt + nativeBuildInputs = + default.nativeBuildInputs + ++ [ + # Always use nightly rustfmt because most of its options are unstable + # + # This needs to come before `toolchain` in this list, otherwise + # `$PATH` will have stable rustfmt instead. + inputs.fenix.packages.${system}.latest.rustfmt - # rust itself - toolchain + # rust itself + toolchain - # CI tests - engage + # CI tests + engage - # format toml files - taplo + # format toml files + taplo - # Needed for producing Debian packages - cargo-deb + # Needed for producing Debian packages + cargo-deb - # Needed for our script for Complement - jq + # Needed for our script for Complement + jq - # Needed for Complement - go - olm + # Needed for Complement + go + olm - # Needed for our script for Complement - jq + # Needed for our script for Complement + jq - # Needed for finding broken markdown links - lychee + # Needed for finding broken markdown links + lychee - # Useful for editing the book locally - mdbook - ]; + # Useful for editing the book locally + mdbook + + # nix formatter + alejandra + ]; }