[M5+] cmd_build merges Cargoxx.lock instead of overwriting
This commit is contained in:
@@ -34,12 +34,27 @@ auto write_text(const fs::path& path, std::string_view content) -> util::Result<
|
||||
return {};
|
||||
}
|
||||
|
||||
// Synthesizes a fresh lockfile from the manifest + resolved recipes. Without a
|
||||
// resolver (M5), `nixpkgs_rev` is left null — flake codegen falls back to the
|
||||
// `nixos-unstable` branch and the user gets a working but non-reproducible
|
||||
// build. M5 will populate the rev.
|
||||
auto synthesize_lockfile(const manifest::Manifest& m,
|
||||
const std::vector<linkdb::Recipe>& recipes) -> lockfile::Lockfile {
|
||||
// Builds the lockfile from the manifest + resolved recipes, **preserving**
|
||||
// `nixpkgs_rev` for any (name, version) entry that already exists in
|
||||
// `prior` with a matching key. This is what makes `cargoxx build`
|
||||
// idempotent w.r.t. the lockfile — concrete pins written by
|
||||
// `cargoxx add <pkg>@<ver>` survive subsequent rebuilds. New deps and
|
||||
// version bumps get a null rev (today's behaviour); the dep tracks the
|
||||
// shared `nixos-unstable` input until a future `cargoxx update`
|
||||
// repopulates it.
|
||||
auto merge_lockfile(const manifest::Manifest& m,
|
||||
const std::vector<linkdb::Recipe>& recipes,
|
||||
const lockfile::Lockfile& prior) -> lockfile::Lockfile {
|
||||
auto find_prior = [&](const std::string& name, const std::string& version)
|
||||
-> std::optional<lockfile::LockfilePackage> {
|
||||
for (const auto& p : prior.packages) {
|
||||
if (p.name == name && p.version == version) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
};
|
||||
|
||||
lockfile::Lockfile lock;
|
||||
lock.version = 1;
|
||||
|
||||
@@ -59,12 +74,16 @@ auto synthesize_lockfile(const manifest::Manifest& m,
|
||||
for (std::size_t i = 0; i < m.dependencies.size(); ++i) {
|
||||
const auto& dep = m.dependencies[i];
|
||||
const auto& rec = recipes[i];
|
||||
std::optional<std::string> rev;
|
||||
if (auto p = find_prior(dep.name, dep.version_spec); p) {
|
||||
rev = p->nixpkgs_rev;
|
||||
}
|
||||
lock.packages.push_back(lockfile::LockfilePackage{
|
||||
.name = dep.name,
|
||||
.version = dep.version_spec,
|
||||
.dependencies = {},
|
||||
.nixpkgs_attr = rec.nixpkgs_attr,
|
||||
.nixpkgs_rev = std::nullopt,
|
||||
.nixpkgs_rev = std::move(rev),
|
||||
.linkdb_source = rec.source,
|
||||
});
|
||||
}
|
||||
@@ -130,7 +149,15 @@ auto cmd_build(const fs::path& project_root, bool no_build, bool release,
|
||||
recipes.push_back(std::move(*r));
|
||||
}
|
||||
|
||||
auto lock = synthesize_lockfile(*m, recipes);
|
||||
lockfile::Lockfile prior;
|
||||
if (std::error_code ec; std::filesystem::exists(project_root / "Cargoxx.lock", ec)) {
|
||||
if (auto r = lockfile::parse(project_root / "Cargoxx.lock"); r) {
|
||||
prior = std::move(*r);
|
||||
}
|
||||
// A malformed prior lockfile is non-fatal — we just rebuild from
|
||||
// scratch. The user can re-pin by running `cargoxx add` again.
|
||||
}
|
||||
auto lock = merge_lockfile(*m, recipes, prior);
|
||||
|
||||
codegen::GenerateInputs in{*m, *layout_result, lock, recipes, project_root};
|
||||
auto flake_text = codegen::flake_nix(in);
|
||||
|
||||
Reference in New Issue
Block a user