Skip to content

Commit

Permalink
refactor(nix): restructure flake
Browse files Browse the repository at this point in the history
This unpacks the flake into a few different files. This more closely
matches the way that I write my projects and separates some concerns out
such as managing devShells alongside cargo builds. Many of the flake
inputs have been updated to take advantage of newer features such as
treefmt in pre-commit-hooks.

Configuration for cocogitto and bomper are also included to handle
releases.
  • Loading branch information
justinrubek committed Oct 6, 2023
1 parent c7c9de5 commit 3359c62
Show file tree
Hide file tree
Showing 11 changed files with 984 additions and 138 deletions.
3 changes: 3 additions & 0 deletions bomp.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(
cargo: Some(Autodetect),
)
23 changes: 23 additions & 0 deletions cog.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ignore_merge_commits = false
branch_whitelist = ["main"]
pre_bump_hooks = [
"bomper {{latest}} {{version}}"
]
post_bump_hooks = [
"git push",
"git push origin {{version}}"
]

[commit_types]

[changelog]
path = "CHANGELOG.md"
template = "remote"
remote = "github.com"
repository = "thoenix"
owner = "justinrubek"
authors = [
{ signature = "Justin Rubek", username = "justinrubek" }
]

[bump_profiles]
155 changes: 59 additions & 96 deletions flake-parts/cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,35 @@
inputs,
self,
...
} @ part-inputs: {
imports = [];

}: {
perSystem = {
pkgs,
lib,
system,
inputs',
self',
...
}: let
devTools = with pkgs; [
# rust tooling
fenix-toolchain
bacon
rustfmt
cargo-nextest
# misc
pkgs.terraform
];

extraNativeBuildInputs = [
pkgs.pkg-config
pkgs.openssl
pkgs.openssl.dev
] ++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.AppKit
pkgs.darwin.apple_sdk.frameworks.CoreFoundation
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.darwin.apple_sdk.frameworks.Foundation
pkgs.darwin.apple_sdk.frameworks.Security
];

# allBuildInputs = base: base ++ extraBuildInputs;
allNativeBuildInputs = base: base ++ extraNativeBuildInputs;

fenix-channel = inputs'.fenix.packages.latest;
fenix-toolchain = fenix-channel.withComponents [
"rustc"
"cargo"
"clippy"
"rust-analysis"
"rust-src"
"rustfmt"
"llvm-tools-preview"
];

craneLib = inputs.crane.lib.${system}.overrideToolchain fenix-toolchain;

common-build-args = rec {
# packages required for building the rust packages
extraPackages =
[
pkgs.pkg-config
pkgs.openssl
pkgs.openssl.dev
]
++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.AppKit
pkgs.darwin.apple_sdk.frameworks.CoreFoundation
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.darwin.apple_sdk.frameworks.Foundation
pkgs.darwin.apple_sdk.frameworks.Security
];
withExtraPackages = base: base ++ extraPackages;

craneLib = inputs.crane.lib.${system}.overrideToolchain self'.packages.rust-toolchain;

commonArgs = rec {
src = inputs.nix-filter.lib {
root = ../.;
include = [
Expand All @@ -63,69 +42,53 @@

pname = "thoenix";

nativeBuildInputs = allNativeBuildInputs [];
nativeBuildInputs = withExtraPackages [];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath nativeBuildInputs;
};
deps-only = craneLib.buildDepsOnly ({} // common-build-args);

clippy-check = craneLib.cargoClippy ({
cargoArtifacts = deps-only;
cargoClippyExtraArgs = "--all-features -- --deny warnings";
}
// common-build-args);

rust-fmt-check = craneLib.cargoFmt ({
inherit (common-build-args) src;
}
// common-build-args);

tests-check = craneLib.cargoNextest ({
cargoArtifacts = deps-only;
partitions = 1;
partitionType = "count";
}
// common-build-args);

pre-commit-hooks = inputs.pre-commit-hooks.lib.${system}.run {
inherit (common-build-args) src;
hooks = {
alejandra.enable = true;
rustfmt.enable = true;
};
};

cli-package = craneLib.buildPackage ({
pname = "thoenix";
cargoArtifacts = deps-only;
cargoExtraArgs = "--bin thoenix";
}
// common-build-args);
in rec {
devShells.default = pkgs.mkShell rec {
packages = allNativeBuildInputs [fenix-toolchain] ++ devTools;
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath packages;
inherit (self.checks.${system}.pre-commit-hooks) shellHook;
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

packages = {
inherit fenix-toolchain;
default = packages.cli;
cli = cli-package;
cli = craneLib.buildPackage ({
pname = "thoenix";
inherit cargoArtifacts;
cargoExtraArgs = "--bin thoenix";
meta.mainProgram = "thoenix";
}
// commonArgs);

cargo-doc = craneLib.cargoDoc ({
inherit cargoArtifacts;
}
// commonArgs);
};

apps = {
cli = {
type = "app";
program = "${self.packages.${system}.cli}/bin/thoenix";
};
default = apps.cli;
checks = {
clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-features -- --deny warnings";
});

rust-fmt = craneLib.cargoFmt (commonArgs
// {
inherit (commonArgs) src;
});

rust-tests = craneLib.cargoNextest (commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
cargoExtraArgs = "--exclude annapurna-wasm --exclude annapurna-ui --workspace";
});
};
in rec {
inherit packages checks;

checks = {
inherit pre-commit-hooks;
clippy = clippy-check;
tests = tests-check;
rust-fmt = rust-fmt-check;
legacyPackages = {
cargoExtraPackages = extraPackages;
};
};
}
34 changes: 34 additions & 0 deletions flake-parts/ci.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{inputs, ...}: {
perSystem = {
config,
pkgs,
system,
inputs',
self',
...
}: let
ciPackages = [
self'.packages.cocogitto
self'.packages.bomper
];

packages = {
cocogitto = pkgs.cocogitto;
bomper = inputs'.bomper.packages.cli;
};

devShells = {
ci = pkgs.mkShell rec {
packages = ciPackages;

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath packages;
};
};
in rec {
inherit devShells packages;

legacyPackages = {
inherit ciPackages;
};
};
}
32 changes: 32 additions & 0 deletions flake-parts/formatting.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
inputs,
self,
...
}: {
perSystem = {
pkgs,
lib,
...
}: let
formatters = [
pkgs.alejandra
pkgs.rustfmt
];

treefmt = pkgs.writeShellApplication {
name = "treefmt";
runtimeInputs = [pkgs.treefmt] ++ formatters;
text = ''
exec treefmt "$@"
'';
};
in {
packages = {
inherit treefmt;
};

legacyPackages = {
inherit formatters;
};
};
}
21 changes: 21 additions & 0 deletions flake-parts/pre-commit.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
inputs,
self,
...
}: {
perSystem = {self', ...}: let
in {
pre-commit = {
check.enable = true;

settings = {
src = ../.;
hooks = {
treefmt.enable = true;
};

settings.treefmt.package = self'.packages.treefmt;
};
};
};
}
38 changes: 38 additions & 0 deletions flake-parts/rust-toolchain.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{...}: {
perSystem = {
inputs',
lib,
pkgs,
...
}: let
# "stable", "latest", "minimal", "complete"
channel = "latest";
fenix-channel = inputs'.fenix.packages.${channel};

# rust targets
fenix-targets = with inputs'.fenix.packages.targets;
[
x86_64-unknown-linux-gnu.${channel}.rust-std
aarch64-unknown-linux-gnu.${channel}.rust-std
]
++ lib.optionals pkgs.stdenv.isDarwin [
x86_64-apple-darwin.${channel}.rust-std
aarch64-apple-darwin.${channel}.rust-std
];

fenix-toolchain = inputs'.fenix.packages.combine ([
fenix-channel.rustc
fenix-channel.cargo
fenix-channel.clippy
fenix-channel.rust-analysis
fenix-channel.rust-src
fenix-channel.rustfmt
fenix-channel.llvm-tools-preview
]
++ fenix-targets);
in {
packages = {
rust-toolchain = fenix-toolchain;
};
};
}
40 changes: 40 additions & 0 deletions flake-parts/shells.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{inputs, ...}: {
perSystem = {
config,
pkgs,
system,
inputs',
self',
lib,
...
}: let
inherit (self'.packages) rust-toolchain;
inherit (self'.legacyPackages) cargoExtraPackages ciPackages;

devTools = [
# rust tooling
rust-toolchain
pkgs.cargo-audit
pkgs.cargo-udeps
pkgs.cargo-nextest
pkgs.bacon
# formatting
self'.packages.treefmt
# misc
pkgs.terraform
];
in {
devShells = {
default = pkgs.mkShell rec {
packages = devTools ++ cargoExtraPackages ++ ciPackages;

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath packages;
RUST_SRC_PATH = "${self'.packages.rust-toolchain}/lib/rustlib/src/rust/src";

shellHook = ''
${config.pre-commit.installationScript}
'';
};
};
};
}
Loading

0 comments on commit 3359c62

Please sign in to comment.