patagia-control/flake.nix

188 lines
5.1 KiB
Nix
Raw Normal View History

2024-10-20 17:29:28 +00:00
{
inputs = {
2024-11-23 19:57:17 +01:00
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
crane.url = "github:ipetkov/crane";
2024-10-20 17:29:28 +00:00
flake-utils.url = "github:numtide/flake-utils";
2024-11-23 19:57:17 +01:00
nix-filter.url = "github:numtide/nix-filter";
2024-12-15 10:17:38 +01:00
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
2024-11-23 19:57:17 +01:00
rust-overlay.url = "github:oxalica/rust-overlay";
treefmt-nix.url = "github:numtide/treefmt-nix";
2024-10-20 17:29:28 +00:00
};
outputs =
{
self,
2024-11-23 19:57:17 +01:00
advisory-db,
crane,
2024-10-20 17:29:28 +00:00
flake-utils,
2024-11-23 19:57:17 +01:00
nix-filter,
2024-10-20 17:29:28 +00:00
nixpkgs,
2024-11-23 19:57:17 +01:00
rust-overlay,
treefmt-nix,
2024-10-20 17:29:28 +00:00
}:
flake-utils.lib.eachDefaultSystem (
system:
let
target = "x86_64-unknown-linux-musl";
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
crossSystem.config = target;
};
staticPkgs = pkgs.pkgsStatic;
2024-11-23 19:57:17 +01:00
craneLib = (crane.mkLib staticPkgs).overrideToolchain (
p:
p.rust-bin.stable.latest.default.override {
targets = [ target ];
}
);
2024-11-23 19:57:17 +01:00
2024-12-15 22:12:59 +01:00
src = pkgs.lib.fileset.toSource {
root = ./.;
fileset = pkgs.lib.fileset.unions [
./api.json
./controller/.sqlx
./controller/migrations
2024-12-15 22:12:59 +01:00
(craneLib.fileset.commonCargoSources ./.)
];
2024-11-23 19:57:17 +01:00
};
commonArgs = {
2024-12-15 22:12:59 +01:00
inherit src;
2024-11-23 19:57:17 +01:00
strictDeps = true;
2024-12-15 22:12:59 +01:00
nativeBuildInputs = with staticPkgs.pkgsBuildHost; [ pkg-config ];
buildInputs = with staticPkgs.pkgsHostHost; [ openssl ];
2024-12-15 22:12:59 +01:00
CARGO_BUILD_TARGET = target;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
"CARGO_TARGET_${pkgs.lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] target)}_LINKER" =
"${staticPkgs.stdenv.cc.targetPrefix}cc";
OPENSSL_STATIC = true;
OPENSSL_DIR = "${staticPkgs.openssl.dev}";
OPENSSL_LIB_DIR = "${staticPkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${staticPkgs.openssl.dev}/include/";
2024-11-23 19:57:17 +01:00
};
2024-12-15 22:12:59 +01:00
buildCrate =
name: path:
craneLib.buildPackage commonArgs
2024-11-23 19:57:17 +01:00
// {
2024-12-15 22:12:59 +01:00
inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
doCheck = false; # We use cargo-nextest for all tests
pname = name;
cargoExtraArgs = "-p ${name}";
2024-12-14 22:47:40 +01:00
};
2024-12-15 22:12:59 +01:00
patagia-agent = buildCrate "patagia-agent" ./agent;
patagia-controller = buildCrate "patagia-controller" ./controller;
hostd = buildCrate "hostd" ./hostd;
2024-12-15 22:12:59 +01:00
xtask = buildCrate "xtask" ./xtask;
2024-10-20 17:29:28 +00:00
in
{
2024-11-23 19:57:17 +01:00
packages = {
inherit
hostd
patagia-agent
patagia-controller
xtask
;
hostd-service =
let
hostd-service = pkgs.writeText "hostd.service" ''
[Unit]
Description=Patagia Hostd
[Service]
Environment=RUST_LOG=debug
ExecStart=${hostd}/bin/hostd
Restart=always
RestartSec=30s
[Install]
WantedBy=multi-user.target
'';
in
pkgs.portableService {
pname = "hostd";
version = "v0.0.1";
units = [ hostd-service ];
};
2024-11-23 19:57:17 +01:00
};
2024-10-20 17:29:28 +00:00
checks = {
inherit
hostd
patagia-agent
patagia-controller
xtask
;
2024-10-20 17:29:28 +00:00
2024-12-15 22:12:59 +01:00
audit = craneLib.cargoAudit (commonArgs // { inherit advisory-db; });
clippy = craneLib.cargoClippy commonArgs // {
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
};
fmt = craneLib.cargoFmt commonArgs;
nextest = craneLib.cargoNextest commonArgs // {
partitions = 1;
partitionType = "count";
};
openapi = pkgs.runCommand "openapi" commonArgs ''
${self.packages.${system}.xtask}/bin/xtask open-api |
${pkgs.diffutils}/bin/diff -u $src/api.json - |
tee $out
'';
2024-10-20 17:29:28 +00:00
};
2024-12-15 22:12:59 +01:00
formatter =
(treefmt-nix.lib.evalModule pkgs {
projectRootFile = "flake.nix";
2024-12-15 22:12:59 +01:00
programs = {
nixfmt.enable = true;
nixfmt.package = pkgs.nixfmt-rfc-style;
shfmt.enable = true;
rustfmt.enable = true;
};
settings.formatter.rustfmt.command = pkgs.lib.mkForce "${pkgs.rust-toolchain}/bin/rustfmt";
}).config.build.wrapper;
2024-11-23 19:57:17 +01:00
2024-10-20 17:29:28 +00:00
devShells.default = pkgs.mkShell {
2024-12-15 22:12:59 +01:00
nativeBuildInputs = commonArgs.nativeBuildInputs;
buildInputs = with pkgs; [
bacon
cargo-edit
cargo-features-manager
cargo-hakari
cargo-machete
cargo-nextest
cargo-watch
hyperfine
just
nixfmt-rfc-style
rust-dev-toolchain
sqls
sqlx-cli
watchexec
];
2024-12-14 22:47:40 +01:00
RUST_BACKTRACE = 1;
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc; # Required for rust-analyzer
2024-10-20 17:29:28 +00:00
};
}
);
}