Skip to content

Commit

Permalink
chore(nix): refactor in preparation for fixing ci
Browse files Browse the repository at this point in the history
  • Loading branch information
DaRacci committed Sep 25, 2023
1 parent f6df373 commit 2c34645
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 99 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]
members = ["crates/*"]
default-members = [
"crates/lib",
"crates/macros",
"crates/backup",
"crates/cleaner",
"crates/country-ip",
Expand Down
173 changes: 94 additions & 79 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
, flake-utils
, crane
, fenix
, depsOnly ? false
}:
let
# TODO: This is a hack to get the right target for the right system.
Expand Down Expand Up @@ -49,97 +48,113 @@ let
then pkgs.pkgsCross.aarch64-multiplatform
else pkgs.pkgsCross.${crossSystem};

inherit (crossPackages) targetPlatform;
isNative = localSystem == crossSystem;
useMold = isNative && targetPlatform.isLinux;
useWine = targetPlatform.isWindows && localSystem == flake-utils.lib.system.x86_64-linux;

commonDeps = {
depsBuildBuild = [ ]
++ lib.optionals (!isNative) (with pkgs; [ qemu ])
++ lib.optionals (targetPlatform.isWindows) (with crossPackages; [ stdenv.cc windows.mingw_w64_pthreads windows.pthreads ]);

buildInputs = with crossPackages; [ openssl ]
++ lib.optionals (useMold) (with pkgs; [ clang mold ]);

nativeBuildInputs = with pkgs; [ pkg-config ]
++ lib.optionals (useWine) ([ (pkgs.wine.override { wineBuild = "wine64"; }) ]);

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [
openssl
] ++ lib.optionals (isNative && targetPlatform.isLinux) (with pkgs; [
wayland
libxkbcommon
vulkan-loader
libglvnd
egl-wayland
wayland-protocols
xwayland
libdecor
]));
};

commonEnv = {
"CARGO_BUILD_TARGET" = target;

"CARGO_TARGET_${TARGET}_LINKER" =
if useMold
then "${crossPackages.clang}/bin/${crossPackages.clang.targetPrefix}clang"
else let inherit (crossPackages.stdenv) cc; in "${cc}/bin/${cc.targetPrefix}cc";

"CARGO_TARGET_${TARGET}_RUSTFLAGS" =
if useMold then "-C link-arg=-fuse-ld=${crossPackages.mold}/bin/mold"
else null;

"CARGO_TARGET_${TARGET}_RUNNER" =
if isNative
then null
else if useWine
then
pkgs.writeScript "wine-wrapper" ''
#!${pkgs.bash}/bin/bash
export WINEPREFIX="$(mktemp -d)"
exec ${(pkgs.wine.override { wineBuild = "wine64"; })}/bin/wine64 $@
''
else "${pkgs.qemu}/bin/qemu-${targetPlatform.qemuArch}";
};

commonArgs =
let
tomlPath = craneLib.path (if workspace == null then ./Cargo.toml else ./crates/${workspace}/Cargo.toml);
cargoToml = builtins.fromTOML (builtins.readFile tomlPath);
inherit (cargoToml.package or { inherit (self) name; version = "0.0.0.0"; }) name version;
cargoToml = craneLib.path (if workspace == null then ./Cargo.toml else ./crates/${workspace}/Cargo.toml);
src = craneLib.path ./.;
in
{
pname = name;
inherit version;
commonDeps // commonEnv // {
inherit src;
inherit (craneLib.crateNameFromCargoToml { inherit src cargoToml; }) pname version;

src = craneLib.path ./.;
cargoLock = craneLib.path ./Cargo.lock;
cargoExtraArgs = if workspace != null then "--package ${workspace}" else "";

strictDeps = true;
};

releaseArgs = commonArgs // {
doCheck = false;
cargoArtifact = craneLib.buildDepsOnly commonArgs;

RUST_LOG = "info";
RUST_LOG_SPAN_EVENTS = null;
};
cargoBuild = artifact: extra: craneLib.buildPackage (commonArgs // {
cargoArtifact = artifact;
} // extra commonArgs);

developmentArgs = commonArgs // {
doCheck = false;
cargoClippy = artifact: extra: craneLib.cargoClippy (commonArgs // {
cargoArtifact = artifact;
cargoClippyExtraArgs = "--package ${workspace} -- --deny warnings";
} // extra commonArgs);

CARGO_PROFILE = "dev";
cargoFmt = artifact: extra: craneLib.cargoFmt (commonArgs // {
cargoArtifact = artifact;
} // extra commonArgs);

RUST_LOG = "trace";
RUST_LOG_SPAN_EVENTS = "full";
cargoTest = artifact: extra: craneLib.cargoNextest (commonArgs // {
cargoArtifact = artifact;
} // extra commonArgs);
in
{
passthru = commonDeps // commonEnv // {
inherit isNative;
};

mkDerivation = args:
let
inherit (crossPackages) targetPlatform;
crateArtifact = cargoArtifact;

isNative = localSystem == crossSystem;
useMold = isNative && targetPlatform.isLinux;
useWine = targetPlatform.isWindows && localSystem == flake-utils.lib.system.x86_64-linux;
in
craneLib.buildPackage (args // {
strictDeps = true;
crateBinary = cargoBuild cargoArtifact (args: {
cargoExtraArgs = "--bin ${args.pname}";
});

passthru = { inherit craneLib args; };

depsBuildBuild = [ ]
++ lib.optionals (!isNative) (with pkgs; [ qemu ])
++ lib.optionals (targetPlatform.isWindows) (with crossPackages; [ stdenv.cc windows.mingw_w64_pthreads windows.pthreads ]);

buildInputs = with crossPackages; [ openssl ]
++ lib.optionals (useMold) (with pkgs; [ clang mold ]);

nativeBuildInputs = with pkgs; [ pkg-config ]
++ lib.optionals (useWine) ([ (pkgs.wine.override { wineBuild = "wine64"; }) ]);

LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath (with pkgs; [
openssl
] ++ lib.optionals (isNative && targetPlatform.isLinux) (with pkgs; [
wayland
libxkbcommon
vulkan-loader
libglvnd
egl-wayland
wayland-protocols
xwayland
libdecor
]));

"CARGO_BUILD_TARGET" = target;

"CARGO_TARGET_${TARGET}_LINKER" =
if useMold
then "${crossPackages.clang}/bin/${crossPackages.clang.targetPrefix}clang"
else let inherit (crossPackages.stdenv) cc; in "${cc}/bin/${cc.targetPrefix}cc";

"CARGO_TARGET_${TARGET}_RUSTFLAGS" =
if useMold then "-C link-arg=-fuse-ld=${crossPackages.mold}/bin/mold"
else null;

"CARGO_TARGET_${TARGET}_RUNNER" =
if isNative
then null
else if useWine
then
pkgs.writeScript "wine-wrapper" ''
#!${pkgs.bash}/bin/bash
export WINEPREFIX="$(mktemp -d)"
exec ${(pkgs.wine.override { wineBuild = "wine64"; })}/bin/wine64 $@
''
else "${pkgs.qemu}/bin/qemu-${targetPlatform.qemuArch}";
});
in
(if depsOnly then
mkDerivation commonArgs
else mkDerivation releaseArgs)
crateLibrary = cargoBuild cargoArtifact (args: {
cargoExtraArgs = "--lib";
});

crateTest = cargoTest cargoArtifact;

crateClippy = cargoClippy cargoArtifact;

crateFmt = cargoFmt cargoArtifact;
}
28 changes: 13 additions & 15 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
hasSubCrates = (builtins.length (cargoToml.workspace.members or [ ])) >= 1;

cargoPackages = onAll localSystem
cargoOutputs = onAll localSystem
(crossSystem:
let
disambiguate = name: if crossSystem == localSystem then name else "${name}-${crossSystem}";
Expand All @@ -41,17 +41,18 @@
memberName = path: let cargo = getCargoToml path; in cargo.package.name;
getPkg = workspace: pkgs.callPackage ./default.nix { inherit localSystem crossSystem flake-utils crane fenix workspace; };
in
if builtins.length members >= 1
then builtins.listToAttrs (builtins.map (member: { name = disambiguate (memberName member); value = let split = builtins.split "/" member; in getPkg (builtins.elemAt split (builtins.length split - 1)); }) members)
else builtins.listToAttrs (builtins.map (member: { name = disambiguate (memberName "crates/${member}"); value = getPkg member; }) (builtins.attrNames (builtins.readDir ./crates)))
if builtins.length members >= 1 then
builtins.listToAttrs (builtins.map (member: { name = disambiguate (memberName member); value = let split = builtins.split "/" member; in getPkg (builtins.elemAt split (builtins.length split - 1)); }) members)
else
builtins.listToAttrs (builtins.map (member: { name = disambiguate (memberName "crates/${member}"); value = getPkg member; }) (builtins.attrNames (builtins.readDir ./crates)))
else { }) // (if ((cargoToml.package.name or null) == null) then { } else (builtins.listToAttrs [{ name = disambiguate "default"; value = pkgs.callPackage ./default.nix { inherit localSystem crossSystem flake-utils crane fenix; }; }]))
);
in
{
packages = cargoPackages // {
packages = builtins.mapAttrs (name: outputs: outputs.crateBinary) cargoOutputs // {
all = pkgs.symlinkJoin {
name = "all";
paths = builtins.attrValues cargoPackages;
paths = builtins.attrValues (builtins.mapAttrs (name: outputs: outputs.crateBinary) cargoOutputs);
};
};

Expand All @@ -63,14 +64,11 @@
(attr: packageChecks: (attr // packageChecks))
{ }
(builtins.attrValues (builtins.mapAttrs
(name: package:
let inherit (package.passthru) craneLib args; in {
"${name}-fmt" = craneLib.cargoFmt args;
"${name}-clippy" = craneLib.cargoClippy (args // {
inherit (package) cargoArtifacts;
cargoClippyExtraArgs = "--workspace -- --deny warnings";
});
})
cargoPackages));
(name: crateOutput: {
"${name}-fmt" = crateOutput.crateFmt;
"${name}-clippy" = crateOutput.crateClippy;
"${name}-test" = crateOutput.crateTest;
})
cargoOutputs));
});
}
8 changes: 5 additions & 3 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
}:

let
mainPkg = pkgs.callPackage ./default.nix { inherit localSystem flake-utils fenix crane; };
inherit (pkgs.callPackage ./default.nix { inherit localSystem flake-utils fenix crane; }) passthru;
fenixPkgs = fenix.packages.${localSystem};
in
mainPkg.overrideAttrs (oa: {
(pkgs.mkShell passthru).overrideAttrs (oldAttrs: {
nativeBuildInputs = with pkgs; [
cocogitto
act
(fenixPkgs.complete.withComponents [
"cargo"
"rust-src"
"rust-analyzer"
"clippy-preview"
Expand All @@ -24,5 +26,5 @@ mainPkg.overrideAttrs (oa: {
cargo-nextest
cargo-expand
cargo-cranky
] ++ (oa.nativeBuildInputs or [ ]);
] ++ (oldAttrs.nativeBuildInputs or [ ]);
})

0 comments on commit 2c34645

Please sign in to comment.