Skip to content

Commit

Permalink
Clean up Nix Flake and packaging (#24)
Browse files Browse the repository at this point in the history
* refactor(nix): split package.nix from flake.nix

* chore(nix): split checks; add more

* ci: run all nix checks

* ci: build in debug mode

* chore(nix): use versionCheckHook

this makes sure the version test is always run, and on the right package

* chore(nix): nix-filter -> lib.fileset

* feat(nix): support stable nix

* ci: fix clippy check
  • Loading branch information
getchoo authored Nov 9, 2024
1 parent 1ec360f commit 0ff3584
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 136 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ jobs:

- name: Run build
run: |
nix build --print-build-logs .#check-version-test
nix build --print-build-logs .#nix-forecast-debug
format:
name: Formatting
format-and-lint:
name: Format and lint

runs-on: ubuntu-latest

Expand All @@ -60,11 +60,11 @@ jobs:

- name: Run flake checks
run: |
nix build --print-build-logs .#check-formatting
nix flake check --print-build-logs --show-trace
release-gate:
name: CI Release gate
needs: [build, format]
needs: [build, format-and-lint]

if: ${{ always() }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/clippy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Run clippy
id: clippy-run
run: |
nix build --print-build-logs .#check-clippy
nix build --print-build-logs .#checks.x86_64-linux.clippy
[ -L result ] || exit 1
echo "sarif-file=$(readlink -f result)" >> "$GITHUB_OUTPUT"
Expand Down
17 changes: 17 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
pkgs ? import nixpkgs {
inherit system;
config = { };
overlays = [ ];
},
lib ? pkgs.lib,
nixpkgs ? <nixpkgs>,
system ? builtins.currentSystem,
}:

let
nixForecastPackages = lib.makeScope pkgs.newScope (lib.flip (import ./overlay.nix) pkgs);
in
{
inherit (nixForecastPackages) nix-forecast;
}
16 changes: 0 additions & 16 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 42 additions & 114 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
{
description = "Check the forecast for today's Nix builds";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nix-filter.url = "github:numtide/nix-filter";
};
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

outputs =
{
self,
nixpkgs,
nix-filter,
}:
let
inherit (nixpkgs) lib;
Expand All @@ -36,16 +32,24 @@
let
pkgs = nixpkgsFor.${system};
packages = self.packages.${system};

mkCheck =
name: deps: script:
pkgs.runCommand name { nativeBuildInputs = deps; } ''
${script}
touch $out
'';
in
lib.optionalAttrs (lib.elem system supportedSystems) {
version-test = packages.nix-forecast.tests.version;

clippy = packages.nix-forecast.overrideAttrs (oldAttrs: {
clippy = packages.nix-forecast.overrideAttrs {
pname = "check-clippy";

nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [
nativeBuildInputs = [
pkgs.cargo
pkgs.clippy
pkgs.clippy-sarif
pkgs.rustPlatform.cargoSetupHook
pkgs.rustc
pkgs.sarif-fmt
];

Expand All @@ -57,36 +61,32 @@
--tests \
--message-format=json \
| clippy-sarif | tee $out | sarif-fmt
runHook postBuild
'';

dontInstall = true;
doCheck = false;
doInstallCheck = false;
dontFixup = true;

passthru = { };
meta = { };
});

formatting =
pkgs.runCommand "check-formatting"
{
nativeBuildInputs = [
pkgs.cargo
pkgs.nixfmt-rfc-style
pkgs.rustfmt
];
}
''
cd ${self}
echo "Running cargo fmt"
cargo fmt -- --check
echo "Running nixfmt..."
nixfmt --check .
touch $out
'';
};

rustfmt = mkCheck "check-cargo-fmt" [
pkgs.cargo
pkgs.rustfmt
] "cd ${self} && cargo fmt -- --check";

actionlint = mkCheck "check-actionlint" [
pkgs.actionlint
] "actionlint ${self}/.github/workflows/*";

deadnix = mkCheck "check-deadnix" [ pkgs.deadnix ] "deadnix --fail ${self}";

nixfmt = mkCheck "check-nixfmt" [ pkgs.nixfmt-rfc-style ] "nixfmt --check ${self}";

statix = mkCheck "check-statix" [ pkgs.statix ] "statix check ${self}";
}
);

Expand Down Expand Up @@ -120,96 +120,24 @@

formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style);

# for CI
legacyPackages = forAllSystems (
system:
lib.optionalAttrs (lib.elem system supportedSystems) (
lib.mapAttrs' (name: lib.nameValuePair "check-${name}") self.checks.${system}
)
);
legacyPackages = forAllSystems (system: {
nix-forecast-debug = self.packages.${system}.nix-forecast.overrideAttrs (
finalAttrs: _: {
cargoBuildType = "debug";
cargoCheckType = finalAttrs.cargoBuildType;
}
);
});

packages = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};
nixForecastPackages = lib.makeScope pkgs.newScope (final: self.overlays.default final pkgs);
pkgs' = import ./default.nix { inherit pkgs; };
in
{
inherit (nixForecastPackages) nix-forecast;
default = self.packages.${system}.nix-forecast;
}
pkgs' // { default = pkgs'.nix-forecast; }
);

overlays.default = final: _: {
nix-forecast = final.callPackage (
{
lib,
stdenv,
rustPlatform,
darwin,
installShellFiles,
makeBinaryWrapper,
nix,
nix-forecast,
testers,
}:

rustPlatform.buildRustPackage rec {
pname = "nix-forecast";
inherit (passthru.cargoTOML.package) version;

src = nix-filter {
root = self;
include = [
./Cargo.toml
./Cargo.lock
./build.rs
"src"
];
};

cargoLock.lockFile = ./Cargo.lock;

nativeBuildInputs = [
installShellFiles
makeBinaryWrapper
];

buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [
darwin.apple_sdk.frameworks.CoreFoundation
darwin.apple_sdk.frameworks.SystemConfiguration
darwin.libiconv
];

postInstall = ''
wrapProgram $out/bin/nix-forecast --suffix PATH : "${lib.makeBinPath [ nix ]}"
installShellCompletion --cmd nix-forecast \
--bash completions/nix-forecast.bash \
--fish completions/nix-forecast.fish \
--zsh completions/_nix-forecast
'';

env = {
COMPLETION_DIR = "completions";
};

passthru = {
cargoTOML = lib.importTOML ./Cargo.toml;

tests.version = testers.testVersion { package = nix-forecast; };
};

meta = {
description = "Check the forecast for today's Nix builds";
homepage = "https://github.com/getchoo/nix-forecast";
changelog = "https://github.com/getchoo/nix-forecast/releases/tag/${version}";
license = lib.licenses.mpl20;
maintainers = with lib.maintainers; [ getchoo ];
mainProgram = "nix-forecast";
};
}
) { };
};
overlays.default = final: prev: import ./overlay.nix final prev;
};
}
64 changes: 64 additions & 0 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
lib,
installShellFiles,
makeBinaryWrapper,
nix,
rustPlatform,
versionCheckHook,
}:

let
fs = lib.fileset;
in
rustPlatform.buildRustPackage rec {
pname = "nix-forecast";
inherit (passthru.cargoTOML.package) version;

src = fs.toSource {
root = ../.;
fileset = fs.intersection (fs.gitTracked ../.) (
fs.unions [
../Cargo.lock
../Cargo.toml
../build.rs
../src
]
);
};

cargoLock.lockFile = ../Cargo.lock;

nativeBuildInputs = [
installShellFiles
makeBinaryWrapper
];

doInstallCheck = true;
nativeInstallCheckInputs = [ versionCheckHook ];

postInstall = ''
wrapProgram $out/bin/nix-forecast --suffix PATH : ${lib.makeBinPath [ nix ]}
installShellCompletion --cmd nix-forecast \
--bash completions/nix-forecast.bash \
--fish completions/nix-forecast.fish \
--zsh completions/_nix-forecast
'';

env = {
COMPLETION_DIR = "completions";
};

passthru = {
cargoTOML = lib.importTOML ../Cargo.toml;
};

meta = {
description = "Check the forecast for today's Nix builds";
homepage = "https://github.com/getchoo/nix-forecast";
changelog = "https://github.com/getchoo/nix-forecast/releases/tag/${version}";
license = lib.licenses.mpl20;
maintainers = with lib.maintainers; [ getchoo ];
mainProgram = "nix-forecast";
};
}
3 changes: 3 additions & 0 deletions overlay.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
final: _: {
nix-forecast = final.callPackage ./nix/package.nix { };
}

0 comments on commit 0ff3584

Please sign in to comment.