Skip to content

Commit

Permalink
feat: use buildRustPackage dream2nix builder
Browse files Browse the repository at this point in the history
  • Loading branch information
yusdacra committed Feb 20, 2022
1 parent def07e4 commit acc807b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 117 deletions.
37 changes: 10 additions & 27 deletions flake.lock

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

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
};

dream2nix = {
url = "github:yusdacra/dream2nix/rust/crane-builder";
url = "github:yusdacra/dream2nix/rust/improvements";
inputs.gomod2nix.follows = "nixpkgs";
inputs.mach-nix.follows = "nixpkgs";
inputs.node2nix.follows = "nixpkgs";
Expand Down
90 changes: 17 additions & 73 deletions src/build.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ let
packageFlag = l.optional (common.memberName != null) "--package ${cargoPkg.name}";
# Specify --features if we have enabled features other than the default ones
featuresFlags = l.optional ((l.length features) > 0) "--features ${(l.concatStringsSep "," features)}";
# Specify --release if release profile is enabled
releaseFlag = l.optional release "--release";
# Member name of the package. Defaults to the crate name in Cargo.toml.
memberName =
if isNull common.memberName
Expand Down Expand Up @@ -65,98 +63,44 @@ let
nativeBuildInputs = l.unique ((prev.nativeBuildInputs or []) ++ common.nativeBuildInputs);
};

# Overrides for the crane builder
craneOverrides =
# Overrides for the build rust package builder
brpOverrides =
let
# Fixup a cargo command for crane
fixupCargoCommand = isDeps: isTest: let
subcmd =
if isTest
then "test"
else "build";
hook =
if isTest
then "Check"
else "Build";

cmd = l.concatStringsSep " " (
["cargo" subcmd]
++ releaseFlag
++ packageFlag
++ featuresFlags
++ (l.optionals (!isTest && !isDeps) [
"--message-format"
"json-render-diagnostics"
">\"$cargoBuildLog\""
])
);
in ''
runHook pre${hook}
echo running: ${l.strings.escapeShellArg cmd}
${
l.optionalString
(!isTest && !isDeps)
"cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)"
}
${cmd}
runHook post${hook}
'';
# Build phase for crane drvs
buildPhase = isDeps: let
p = fixupCargoCommand false isDeps;
in
l.dbgX "${l.optionalString isDeps "deps-"}buildPhase" p;
# Check phase for crane drvs
checkPhase = isDeps: let
p = fixupCargoCommand true isDeps;
in
l.dbgX "${l.optionalString isDeps "deps-"}checkPhase" p;

# Overrides for the dependency only drv
depsOverride = prev:
l.applyOverrides prev [
(prev: {
doCheck = false;
buildPhase = buildPhase true;
checkPhase = checkPhase true;
})
commonDepsOv
common.internal.crateOverridesCombined
];
# Overrides for the main drv
mainOverride = prev:
flags = l.concatStringsSep " " (packageFlag ++ featuresFlags);
profile =
if release
then "release"
else "debug";
# Overrides for the drv
overrides = prev:
l.applyOverrides prev [
(prev: {
inherit doCheck;
meta = common.meta;
dontFixup = !release;
buildPhase = buildPhase false;
checkPhase = checkPhase false;
cargoBuildFlags = flags;
cargoCheckFlags = flags;
cargoBuildType = profile;
cargoCheckType = profile;
})
desktopItemOv
runtimeLibsOv
commonDepsOv
common.internal.mainBuildOverride
common.internal.crateOverridesCombined
];
in {
"${cargoPkg.name}-deps" = {
nci-overrides.overrideAttrs = prev: let
data = depsOverride prev;
in
l.dbgX "deps override diff" data;
};
${cargoPkg.name} = {
nci-overrides.overrideAttrs = prev: let
data = mainOverride prev;
data = overrides prev;
in
l.dbgX "main override diff" data;
l.dbgX "overrided drv" data;
};
};

# TODO: support dream2nix builder switching
baseConfig = {
inherit root memberName;
packageOverrides = craneOverrides;
packageOverrides = brpOverrides;
};

overrideConfig = config:
Expand Down
16 changes: 1 addition & 15 deletions src/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,9 @@ let
# Combine all crate overrides into one big override function, except the main crate override
crateOverridesCombined =
let
filteredOverrides = l.removeAttrs noPropagatedEnvOverrides [cargoPkg.name];
func = prev:
prev
// (l.pipe prev (
l.map
(ov: (old: old // (ov old)))
(l.attrValues filteredOverrides)
));
func = prev: l.applyOverrides prev (l.attrValues noPropagatedEnvOverrides);
in
l.dbgXY "combined overrides diff" (func {}) func;
# The main crate override is taken here
mainBuildOverride =
let
ov = prev: prev // ((noPropagatedEnvOverrides.${cargoPkg.name} or (_: {})) prev);
in
l.dbgXY "main override diff" (ov {}) ov;

# TODO: try to convert cargo maintainers to nixpkgs maintainers
meta =
Expand Down Expand Up @@ -200,7 +187,6 @@ let
crateOverridesCombined
noPropagatedEnvOverrides
isRootMember
mainBuildOverride
crateOverridesGetFlattenLists
;

Expand Down
11 changes: 10 additions & 1 deletion src/pkgs-set.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ rec {
cargo = toolchain;
};
# dream2nix tools
dream2nix = sources.dream2nix.lib.init { pkgs = pkgs // rustToolchain; };
dream2nix = sources.dream2nix.lib.init {
pkgs =
pkgs
// rustToolchain
// {
rustPlatform = pkgs.makeRustPlatform {
inherit (rustToolchain) rustc cargo;
};
};
};
# devshell
makeDevshell = import "${sources.devshell}/modules" pkgs;
# nci library utilities
Expand Down

0 comments on commit acc807b

Please sign in to comment.