forked from zhaofengli/attic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crane.nix
169 lines (137 loc) · 5.04 KB
/
crane.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# For distribution from this repository as well as CI, we use Crane to build
# Attic.
#
# For a nixpkgs-acceptable form of the package expression, see `package.nixpkgs.nix`
# which will be submitted when the Attic API is considered stable. However, that
# expression is not tested by CI so to not slow down the hot path.
{ stdenv
, clangStdenv
, lib
, craneLib
, rustPlatform
, runCommand
, writeReferencesToFile
, pkg-config
, installShellFiles
, jq
, nix
, boost
, darwin
, libiconv
, libcxx
, xz
}:
let
version = "0.1.0";
ignoredPaths = [ ".github" "target" "book" "nixos" "integration-tests" ];
src = lib.cleanSourceWith {
filter = name: type: !(type == "directory" && builtins.elem (baseNameOf name) ignoredPaths);
src = lib.cleanSource ./.;
};
nativeBuildInputs = [
pkg-config
installShellFiles
];
buildInputs = [
nix boost xz
] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.SystemConfiguration
libiconv
];
cargoArtifacts = craneLib.buildDepsOnly {
pname = "attic";
inherit src version nativeBuildInputs buildInputs;
# By default it's "use-symlink", which causes Crane's `inheritCargoArtifactsHook`
# to copy the artifacts using `cp --no-preserve=mode` which breaks the executable
# bit of bindgen's build-script binary.
#
# With `use-zstd`, the cargo artifacts are archived in a `tar.zstd`. This is
# actually set if you use `buildPackage` without passing `cargoArtifacts`.
installCargoArtifactsMode = "use-zstd";
};
mkAttic = args: craneLib.buildPackage ({
pname = "attic";
inherit src version nativeBuildInputs buildInputs cargoArtifacts;
ATTIC_DISTRIBUTOR = "attic";
# Workaround for https://github.com/NixOS/nixpkgs/issues/166205
env = lib.optionalAttrs (stdenv.cc.isClang && !stdenv.isFreeBSD) {
NIX_LDFLAGS = "-l${stdenv.cc.libcxx.cxxabi.libName}";
} // lib.optionalAttrs (stdenv.cc.isGNU && stdenv.isFreeBSD) {
# positively nightmarish
NIX_LDFLAGS = "-lstdc++ -L${libcxx}/lib";
} // lib.optionalAttrs stdenv.isFreeBSD {
NIX_CFLAGS_COMPILE = "-I${nix.dev}/include/nix";
};
# See comment in `attic-tests`
doCheck = false;
cargoExtraArgs = "-p attic-client -p attic-server";
postInstall = lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
if [[ -f $out/bin/attic ]]; then
installShellCompletion --cmd attic \
--bash <($out/bin/attic gen-completions bash) \
--zsh <($out/bin/attic gen-completions zsh) \
--fish <($out/bin/attic gen-completions fish)
fi
'';
meta = with lib; {
description = "Multi-tenant Nix binary cache system";
homepage = "https://github.com/zhaofengli/attic";
license = licenses.asl20;
maintainers = with maintainers; [ zhaofengli ];
platforms = platforms.linux ++ platforms.darwin ++ platforms.freebsd;
};
} // args);
attic = mkAttic {
cargoExtraArgs = "-p attic-client -p attic-server";
};
# Client-only package.
attic-client = mkAttic {
cargoExtraArgs = " -p attic-client";
};
# Server-only package with fat LTO enabled.
#
# Because of Cargo's feature unification, the common `attic` crate always
# has the `nix_store` feature enabled if the client and server are built
# together, leading to `atticd` linking against `libnixstore` as well. This
# package is slimmer with more optimization.
#
# We don't enable fat LTO in the default `attic` package since it
# dramatically increases build time.
attic-server = craneLib.buildPackage {
pname = "attic-server";
# We don't pull in the common cargoArtifacts because the feature flags
# and LTO configs are different
inherit src version nativeBuildInputs buildInputs;
# See comment in `attic-tests`
doCheck = false;
cargoExtraArgs = "-p attic-server";
CARGO_PROFILE_RELEASE_LTO = "fat";
CARGO_PROFILE_RELEASE_CODEGEN_UNITS = "1";
};
# Attic interacts with Nix directly and its tests require trusted-user access
# to nix-daemon to import NARs, which is not possible in the build sandbox.
# In the CI pipeline, we build the test executable inside the sandbox, then
# run it outside.
attic-tests = craneLib.mkCargoDerivation {
pname = "attic-tests";
inherit src version buildInputs cargoArtifacts;
nativeBuildInputs = nativeBuildInputs ++ [ jq ];
# Workaround for https://github.com/NixOS/nixpkgs/issues/166205
env = lib.optionalAttrs stdenv.cc.isClang {
NIX_LDFLAGS = "-l${stdenv.cc.libcxx.cxxabi.libName}";
};
doCheck = true;
buildPhaseCargoCommand = "";
checkPhaseCargoCommand = "cargoWithProfile test --no-run --message-format=json >cargo-test.json";
doInstallCargoArtifacts = false;
installPhase = ''
runHook preInstall
mkdir -p $out/bin
jq -r 'select(.reason == "compiler-artifact" and .target.test and .executable) | .executable' <cargo-test.json | \
xargs -I _ cp _ $out/bin
runHook postInstall
'';
};
in {
inherit cargoArtifacts attic attic-client attic-server attic-tests;
}