Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nix): fix flake lock refresh #33991

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 134 additions & 23 deletions lib/modules/manager/nix/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,23 @@ 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 () => {
fs.readLocalFile.mockResolvedValueOnce(flake1Lock);
expect(await extractPackageFile('', 'flake.nix')).toBeNull();
expect(await extractPackageFile('', 'flake.lock')).toBeNull();
});

const flake2Lock = `{
Expand Down Expand Up @@ -120,12 +121,12 @@ describe('modules/manager/nix/extract', () => {

it('returns nixpkgs input', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake2Lock);
expect(await extractPackageFile('', 'flake.nix')).toEqual({
expect(await extractPackageFile('', 'flake.lock')).toEqual({
deps: [
{
depName: 'nixpkgs',
currentDigest: '9f4128e00b0ae8ec65918efeba59db998750ead6',
currentValue: 'nixos-unstable',
currentDigest: '9f4128e00b0ae8ec65918efeba59db998750ead6',
datasource: GitRefsDatasource.id,
packageName: 'https://github.com/NixOS/nixpkgs',
},
Expand Down Expand Up @@ -162,7 +163,7 @@ describe('modules/manager/nix/extract', () => {

it('includes nixpkgs with no explicit ref', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake3Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '612ee628421ba2c1abca4c99684862f76cb3b089',
Expand Down Expand Up @@ -221,7 +222,7 @@ describe('modules/manager/nix/extract', () => {

it('includes patchelf from HEAD', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake4Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: 'a0f54334df36770b335c051e540ba40afcbf8378',
Expand Down Expand Up @@ -263,7 +264,7 @@ describe('modules/manager/nix/extract', () => {

it('includes ijq from sourcehut without a flake', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake5Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '88f0d9ae98942bf49cba302c42b2a0f6e05f9b58',
Expand Down Expand Up @@ -305,7 +306,7 @@ describe('modules/manager/nix/extract', () => {

it('includes home-manager from gitlab', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake6Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '65ae9c147349829d3df0222151f53f79821c5134',
Expand All @@ -327,7 +328,7 @@ describe('modules/manager/nix/extract', () => {

it('test other version', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake7Lock);
expect(await extractPackageFile('', 'flake.nix')).toBeNull();
expect(await extractPackageFile('', 'flake.lock')).toBeNull();
});

const flake8Lock = `{
Expand Down Expand Up @@ -361,7 +362,7 @@ describe('modules/manager/nix/extract', () => {

it('includes nixpkgs with ref and shallow arguments', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake8Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '5633bcff0c6162b9e4b5f1264264611e950c8ec7',
Expand Down Expand Up @@ -401,7 +402,7 @@ describe('modules/manager/nix/extract', () => {

it('includes nixpkgs but using indirect type that cannot be updated', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake9Lock);
expect(await extractPackageFile('', 'flake.nix')).toBeNull();
expect(await extractPackageFile('', 'flake.lock')).toBeNull();
});

const flake10Lock = `{
Expand Down Expand Up @@ -488,7 +489,7 @@ describe('modules/manager/nix/extract', () => {

it('includes flake from GitHub Enterprise', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake10Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: '6bf2706348447df6f8b86b1c3e54f87b0afda84f',
Expand Down Expand Up @@ -591,7 +592,7 @@ describe('modules/manager/nix/extract', () => {

it('includes flake with tarball type', async () => {
fs.readLocalFile.mockResolvedValueOnce(flake11Lock);
expect(await extractPackageFile('', 'flake.nix')).toMatchObject({
expect(await extractPackageFile('', 'flake.lock')).toMatchObject({
deps: [
{
currentDigest: 'c7e39452affcc0f89e023091524e38b3aaf109e9',
Expand All @@ -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',
},
],
});
});
});
38 changes: 25 additions & 13 deletions lib/modules/manager/nix/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ export async function extractPackageFile(

const deps: PackageDependency[] = [];

const match = nixpkgsRegex.exec(content);
if (match?.groups) {
const { ref } = match.groups;
deps.push({
depName: 'nixpkgs',
currentValue: ref,
datasource: GitRefsDatasource.id,
packageName: 'https://github.com/NixOS/nixpkgs',
versioning: nixpkgsVersioning,
});
if (packageFile.match(regEx(/\.nix$/))) {
const nixpkgsMatch = nixpkgsRegex.exec(content);
if (nixpkgsMatch?.groups) {
const { ref } = nixpkgsMatch.groups;
// 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 };
}
}
}
return null;
}

const flakeLockParsed = NixFlakeLock.safeParse(lockContents);
Expand All @@ -55,9 +65,6 @@ export async function extractPackageFile(
`flake.lock is missing "root" node`,
);

if (deps.length) {
return { deps };
}
return null;
}

Expand Down Expand Up @@ -89,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/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const supportsLockFileMaintenance = true;
export const url = 'https://nix.dev';

export const defaultConfig = {
fileMatch: ['(^|/)flake\\.nix$'],
fileMatch: ['(^|/)flake\\.(nix|lock)$'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this approach. I think we need a different solution. instead of writing the version from lockfile to current version, use locked version property. this should refuse renovate from trying auto replace for such dependencies

lockedVersion?: string;

commitMessageTopic: 'nix',
commitMessageExtra: 'to {{newValue}}',
enabled: false,
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