-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
113 lines (104 loc) · 4.45 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# SPDX-FileCopyrightText: 2021 localthomas
#
# SPDX-License-Identifier: MIT OR Apache-2.0
{
description = "This is a standalone binary that listens on the system bus and talks to systemd to identify failed units.";
inputs = {
# for eachSystem function
flake-utils.url = "github:numtide/flake-utils";
# use flake-compat as side-effect for flake.lock file that is read by shell.nix
# fill the flake.lock file with `nix flake lock --update-input flake-compat`
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
# get the rust toolchain
rust-overlay.url = "github:oxalica/rust-overlay";
# use the rust toolchain for building the binary
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, naersk, ... }:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let
cargo-metadata = (builtins.fromTOML (builtins.readFile ./Cargo.toml));
crateName = cargo-metadata.package.name;
# apply the rust-overlay to nixpkgs
pkgs = import nixpkgs {
inherit system;
overlays = [ (import rust-overlay) ];
};
# setup the rust toolchain based on the rust-toolchain file
rust = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
# Override the version used in naersk and using the toolchain from above
naersk-lib = naersk.lib."${system}".override {
cargo = rust;
rustc = rust;
};
in
with pkgs;
{
devShells.default = mkShell {
# tools and dependencies for building and developing
nativeBuildInputs = [ nixpkgs-fmt rust cargo-about reuse ];
};
checks = {
format = runCommand "check-format"
{
nativeBuildInputs = [ self.devShells.${system}.default.nativeBuildInputs ];
}
''
cargo-fmt fmt --manifest-path ${./.}/Cargo.toml -- --check
nixpkgs-fmt --check ${./.}
touch $out # touch output file to give the information that check was successful
'';
reuse = runCommand "check-reuse"
{
nativeBuildInputs = [ self.devShells.${system}.default.nativeBuildInputs ];
}
''
reuse --root ${./.} lint
touch $out # touch output file to give the information that check was successful
'';
};
packages."${crateName}-image" = dockerTools.buildImage
{
name = crateName;
tag = "latest";
created = "now";
copyToRoot = self.packages.${system}.${crateName};
config = {
# Note that the entrypoint is *not* "${self.packages.${system}.${crateName}}/bin/${crateName}"
Entrypoint = [ "./bin/${crateName}" ];
Volumes = { "/var/lib/systemd-fail-notifications" = { }; };
Labels = {
# Optional annotations for OCI images (https://github.com/opencontainers/image-spec/blob/main/annotations.md)
"org.opencontainers.image.title" = cargo-metadata.package.name;
"org.opencontainers.image.description" = cargo-metadata.package.description;
"org.opencontainers.image.authors" = builtins.concatStringsSep "; " cargo-metadata.package.authors;
"org.opencontainers.image.version" = cargo-metadata.package.version;
"org.opencontainers.image.source" = cargo-metadata.package.repository;
"org.opencontainers.image.licenses" = cargo-metadata.package.license;
} //
# Optional revision label for the image
(lib.optionalAttrs (self ? rev) {
"org.opencontainers.image.revision" = self.rev;
});
};
};
packages.${crateName} = naersk-lib.buildPackage {
pname = crateName;
root = ./.;
# The packages of the devShell are re-used for building
nativeBuildInputs = [ self.devShells.${system}.default.nativeBuildInputs ];
# Configures the target which will be built.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";
doCheck = true;
};
packages.default = self.packages.${system}.${crateName};
}
);
}