Skip to content

Commit

Permalink
fix(nix): handle nixpkgs upgrades with not explicit ref correct
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Feb 5, 2025
1 parent 0ba64df commit 1270283
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 22 deletions.
133 changes: 122 additions & 11 deletions lib/modules/manager/nix/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ describe('modules/manager/nix/extract', () => {

it('includes nixpkgs input with no explicit ref', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake1Lock);
expect(await extractPackageFile(flake4Nix, 'flake.nix')).toEqual({
deps: [
{
currentValue: undefined,
datasource: 'git-refs',
depName: 'nixpkgs',
packageName: 'https://github.com/NixOS/nixpkgs',
versioning: 'nixpkgs',
},
],
});
expect(await extractPackageFile(flake4Nix, 'flake.nix')).toBeNull();
});

const flake5Nix = `{
inputs = {
nixpkgs-lib.url = "https://github.com/NixOS/nixpkgs/archive/072a6db25e947df2f31aab9eccd0ab75d5b2da11.tar.gz";
};
}`;

it('includes nixpkgs input with only ref', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake1Lock);
expect(await extractPackageFile(flake5Nix, 'flake.nix')).toBeNull();
});

it('returns null when no inputs', async () => {
Expand Down Expand Up @@ -602,4 +603,114 @@ describe('modules/manager/nix/extract', () => {
],
});
});

const flake12Lock = `{
"nodes": {
"nixpkgs-lib": {
"locked": {
"lastModified": 1738452942,
"narHash": "sha256-vJzFZGaCpnmo7I6i416HaBLpC+hvcURh/BQwROcGIp8=",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/072a6db25e947df2f31aab9eccd0ab75d5b2da11.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/072a6db25e947df2f31aab9eccd0ab75d5b2da11.tar.gz"
}
},
"root": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
}
}
},
"root": "root",
"version": 7
}`;

it('includes flake with only tarball type', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake12Lock);
expect(await extractPackageFile('', 'flake.lock')).toBeNull();
});

const flake13Lock = `{
"nodes": {
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1733312601,
"narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1734649271,
"narHash": "sha256-4EVBRhOjMDuGtMaofAIqzJbg4Ql7Ai0PSeuVZTHjyKQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "d70bd19e0a38ad4790d3913bf08fcbfc9eeca507",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1733096140,
"narHash": "sha256-1qRH7uAUsyQI7R1Uwl4T+XvdNv778H0Nb5njNrqvylY=",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/5487e69da40cbd611ab2cadee0b4637225f7cfae.tar.gz"
}
},
"root": {
"inputs": {
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}`;

it('includes flake with nixpkgs-lib as tarball type', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake13Lock);
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9',
currentValue: undefined,
datasource: 'git-refs',
depName: 'flake-parts',
packageName: 'https://github.com/hercules-ci/flake-parts',
},
{
currentDigest: 'd70bd19e0a38ad4790d3913bf08fcbfc9eeca507',
currentValue: 'nixos-unstable',
datasource: 'git-refs',
depName: 'nixpkgs',
packageName: 'https://github.com/nixos/nixpkgs',
},
],
});
});
});
28 changes: 18 additions & 10 deletions lib/modules/manager/nix/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ export async function extractPackageFile(
const nixpkgsMatch = nixpkgsRegex.exec(content);
if (nixpkgsMatch?.groups) {
const { ref } = nixpkgsMatch.groups;
deps.push({
depName: 'nixpkgs',
currentValue: ref,
datasource: GitRefsDatasource.id,
packageName: 'https://github.com/NixOS/nixpkgs',
versioning: nixpkgsVersioning,
});
}
// only add when we matched a ref
if (ref !== undefined) {
deps.push({
depName: 'nixpkgs',
currentValue: ref,
datasource: GitRefsDatasource.id,
packageName: 'https://github.com/NixOS/nixpkgs',
versioning: nixpkgsVersioning,
});

if (deps.length) {
return { deps };
if (deps.length) {
return { deps };
}
}
}
return null;
}
Expand Down Expand Up @@ -93,6 +96,11 @@ export async function extractPackageFile(
continue;
}

// if no rev is being tracked, we cannot update this input
if (flakeLocked.rev === undefined) {
continue;
}

switch (flakeLocked.type) {
case 'github':
deps.push({
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/nix/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const InputType = z.enum([

const LockedInput = z.object({
ref: z.string().optional(),
rev: z.string(),
rev: z.string().optional(),
type: InputType,
});

Expand Down

0 comments on commit 1270283

Please sign in to comment.