Skip to content

linux/kernel: support empty-string values via lib.kernel.freeform "" - #517382

Merged
doronbehar merged 1 commit into
NixOS:stagingfrom
JamieMagee:kernel-config/freeform-empty-string
Sep 2, 2026
Merged

doronbehar merged 1 commit into
NixOS:stagingfrom
JamieMagee:kernel-config/freeform-empty-string

Conversation

@JamieMagee

@JamieMagee JamieMagee commented May 6, 2026 •

Copy link
Copy Markdown
Member

lib.kernel.freeform "" currently produces CONFIG_FOO="\"\"". The intermediate config writes "" as the answer, and generate-config.pl passes those quote characters to Kconfig instead of setting an empty value.

This PR represents an empty value as an option with no value:

 REQUIRED_EMPTY
 OPTIONAL_EMPTY?

generate-config.pl records these as empty strings. After both make config runs, it rewrites matching assignments in .config to CONFIG_<name>="".

Resolves #516936.

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

nixpkgs-branch-check[bot]

This comment was marked as outdated.

@nixpkgs-ci
nixpkgs-ci Bot requested a review from a team May 6, 2026 17:44
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: kernel The Linux kernel 8.has: module (update) This PR changes an existing module in `nixos/` labels May 6, 2026
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from 1e215ba to 8eaabfd Compare May 6, 2026 17:45
@JamieMagee
JamieMagee changed the base branch from master to staging May 6, 2026 17:46
@nixpkgs-ci nixpkgs-ci Bot closed this May 6, 2026
nixpkgs-branch-check[bot]

This comment was marked as outdated.

@JamieMagee
JamieMagee requested a review from alyssais May 10, 2026 21:56
@alyssais
alyssais requested a review from a team May 18, 2026 14:56
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from 8240730 to 34596e4 Compare May 20, 2026 03:08
@nixpkgs-branch-check
nixpkgs-branch-check Bot dismissed stale reviews from themself May 20, 2026 03:09

Review dismissed automatically

@JamieMagee
JamieMagee requested review from alyssais and doronbehar May 20, 2026 03:09

@doronbehar doronbehar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small Perl suggestion, shouldn't be a blocker though.

@doronbehar

Copy link
Copy Markdown
Contributor

Friendly ping @JamieMagee

@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from 34596e4 to d8e5a96 Compare May 26, 2026 20:00
@JamieMagee
JamieMagee requested a review from doronbehar May 26, 2026 20:00

@doronbehar doronbehar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes look good but maybe something got broken at some point - I tested with the following change to nixos-raspberrypi:

diff --git i/flake.lock w/flake.lock
index 8793b4b..f41edc3 100644
--- i/flake.lock
+++ w/flake.lock
@@ -57,16 +57,16 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1778737229,
-        "narHash": "sha256-6xWoytx8jFW4PF1GjRm/i/53trbpKGfz6zjzQGBr4cI=",
-        "owner": "NixOS",
+        "lastModified": 1779887054,
+        "narHash": "sha256-qJ1s23xZwPREMIBIpt7TA1ITXE1Vu/g8uYHK/KTWRSI=",
+        "owner": "doronbehar",
         "repo": "nixpkgs",
-        "rev": "d7a713c0b7e47c908258e71cba7a2d77cc8d71d5",
+        "rev": "00fac792b1f67a1d91395d21c3a45b5805962b45",
         "type": "github"
       },
       "original": {
-        "owner": "NixOS",
-        "ref": "nixos-25.11",
+        "owner": "doronbehar",
+        "ref": "jamieMagee-kernel-config@master",
         "repo": "nixpkgs",
         "type": "github"
       }
diff --git i/flake.nix w/flake.nix
index 0e10e90..2ef4591 100644
--- i/flake.nix
+++ w/flake.nix
@@ -1,18 +1,18 @@
 {
   description = "Flake for RaspberryPi support on NixOS";
 
-  nixConfig = {
-    extra-substituters = [
-      "https://nixos-raspberrypi.cachix.org"
-    ];
-    extra-trusted-public-keys = [
-      "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
-    ];
-    connect-timeout = 5;
-  };
+  #nixConfig = {
+  #  extra-substituters = [
+  #    "https://nixos-raspberrypi.cachix.org"
+  #  ];
+  #  extra-trusted-public-keys = [
+  #    "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
+  #  ];
+  #  connect-timeout = 5;
+  #};
 
   inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
+    nixpkgs.url = "github:doronbehar/nixpkgs/jamieMagee-kernel-config@master";
 
     argononed = {
       # url = "git+file:../argononed?shallow=1";
diff --git i/pkgs/linux-rpi/kernels.nix w/pkgs/linux-rpi/kernels.nix
index f08a776..1b15b58 100644
--- i/pkgs/linux-rpi/kernels.nix
+++ w/pkgs/linux-rpi/kernels.nix
@@ -63,6 +63,8 @@ listToAttrsWLVer [
             NFS_V4 = yes; # =module in nixos;
             NLS_CODEPAGE_437 = mkKernelOverride yes; # =module in nixos;
             ROOT_NFS = yes;
+            # Requires https://github.com/NixOS/nixpkgs/pull/517382
+            CONFIG_LOCALVERSION="";
             # UEVENT_HELPER = yes;
             # UNICODE = module; # =y in nixos
             # USB_SERIAL = module ; # =yes in nixos;
diff --git i/pkgs/linux-rpi/package.nix w/pkgs/linux-rpi/package.nix
index 1ce080e..97c11be 100644
--- i/pkgs/linux-rpi/package.nix
+++ w/pkgs/linux-rpi/package.nix
@@ -98,12 +98,6 @@ in
   // (args.argsOverride or { })
 )).overrideAttrs
   {
-    postConfigure = ''
-      # The v7 defconfig has this set to '-v7' which screws up our modDirVersion.
-      sed -i $buildRoot/.config -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-      sed -i $buildRoot/include/config/auto.conf -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-    '';
-
     postFixup =
       let
         armArch = if stdenv.isAarch64 then "arm64" else "arm";

And the following command (inside nixos-raspberrypi):

nix build -Lf. packages.aarch64-linux.linux-rpi3

And got:

Error: modDirVersion 6.12.87 specified in the Nix expression is wrong, it should be: 6.12.87-v8

Where the nixpkgs fork is this PR merged upon branch master:

  • github.com/NixOS/nixpkgs/compare/master...doronbehar:jamieMagee-kernel-config@master

@doronbehar

Copy link
Copy Markdown
Contributor

Still not working:

Error: modDirVersion 6.12.87 specified in the Nix expression is wrong, it should be: 6.12.87-v8

With this nixos-raspberrypi diff:

diff --git i/flake.lock w/flake.lock
index 8793b4b..172ac65 100644
--- i/flake.lock
+++ w/flake.lock
@@ -57,16 +57,16 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1778737229,
-        "narHash": "sha256-6xWoytx8jFW4PF1GjRm/i/53trbpKGfz6zjzQGBr4cI=",
-        "owner": "NixOS",
+        "lastModified": 1780903365,
+        "narHash": "sha256-ooWyCnDTd780PBIJSspLxZ8mFjzmucl4K5aW2BQMxBk=",
+        "owner": "doronbehar",
         "repo": "nixpkgs",
-        "rev": "d7a713c0b7e47c908258e71cba7a2d77cc8d71d5",
+        "rev": "6e402ddd20ac144d8f225fbc1d1dbb5d4f7a5c45",
         "type": "github"
       },
       "original": {
-        "owner": "NixOS",
-        "ref": "nixos-25.11",
+        "owner": "doronbehar",
+        "ref": "tmp/jamieMagee-kernel-config@master",
         "repo": "nixpkgs",
         "type": "github"
       }
diff --git i/flake.nix w/flake.nix
index 0e10e90..3e087cd 100644
--- i/flake.nix
+++ w/flake.nix
@@ -1,18 +1,18 @@
 {
   description = "Flake for RaspberryPi support on NixOS";
 
-  nixConfig = {
-    extra-substituters = [
-      "https://nixos-raspberrypi.cachix.org"
-    ];
-    extra-trusted-public-keys = [
-      "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
-    ];
-    connect-timeout = 5;
-  };
+  #nixConfig = {
+  #  extra-substituters = [
+  #    "https://nixos-raspberrypi.cachix.org"
+  #  ];
+  #  extra-trusted-public-keys = [
+  #    "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
+  #  ];
+  #  connect-timeout = 5;
+  #};
 
   inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
+    nixpkgs.url = "github:doronbehar/nixpkgs/tmp/jamieMagee-kernel-config@master";
 
     argononed = {
       # url = "git+file:../argononed?shallow=1";
diff --git i/pkgs/linux-rpi/kernels.nix w/pkgs/linux-rpi/kernels.nix
index f08a776..bf903ad 100644
--- i/pkgs/linux-rpi/kernels.nix
+++ w/pkgs/linux-rpi/kernels.nix
@@ -63,6 +63,8 @@ listToAttrsWLVer [
             NFS_V4 = yes; # =module in nixos;
             NLS_CODEPAGE_437 = mkKernelOverride yes; # =module in nixos;
             ROOT_NFS = yes;
+            # Requires https://github.com/NixOS/nixpkgs/pull/517382
+            LOCALVERSION = freeform "";
             # UEVENT_HELPER = yes;
             # UNICODE = module; # =y in nixos
             # USB_SERIAL = module ; # =yes in nixos;
diff --git i/pkgs/linux-rpi/package.nix w/pkgs/linux-rpi/package.nix
index 1ce080e..97c11be 100644
--- i/pkgs/linux-rpi/package.nix
+++ w/pkgs/linux-rpi/package.nix
@@ -98,12 +98,6 @@ in
   // (args.argsOverride or { })
 )).overrideAttrs
   {
-    postConfigure = ''
-      # The v7 defconfig has this set to '-v7' which screws up our modDirVersion.
-      sed -i $buildRoot/.config -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-      sed -i $buildRoot/include/config/auto.conf -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-    '';
-
     postFixup =
       let
         armArch = if stdenv.isAarch64 then "arm64" else "arm";

Now I notice that the postConfigure hook I delete mentions -v7, while the error refers to -v8 - not sure if that's a clue to what's wrong here.

@JamieMagee

Copy link
Copy Markdown
Member Author

@doronbehar OK, I think I've got it, and the PR itself is fine. The issue is where the override ended up in your tree.

I reproduced the full flow against the rpi source: a defconfig forced to CONFIG_LOCALVERSION="-v8" plus an answer file carrying the sentinel. make config does keep -v8 (an empty interactive answer won't override a non-empty default), but the strip pass then rewrites .config and auto.conf to CONFIG_LOCALVERSION="". Running that through make oldconfig and make prepare, kernel.release comes out as plain 6.12.75, no suffix. The Nix side checks out too (freeform "" evaluates to LOCALVERSION __NIXPKGS_EMPTY_STRING__).

The reason you're still on -v8: your build is 6.12.87, but LOCALVERSION = freeform "" is in the common block of fixupStructuredConfig, and that block only gets attached to linux.v6_12_25. v6_12_87 is a bare entry with no fixup, so the override never reaches it and you fall back to the defconfig's -v8.

The sed you deleted lived in package.nix's postConfigure and ran for every kernel. Its replacement needs that same global spot. In mkConfig:

structuredExtraConfig = configFixup // genericConfig // {
  LOCALVERSION = lib.kernel.freeform "";
};

That applies it to all versions and the modDirVersion check should pass. The -v7 vs -v8 detail is a red herring by the way, just a different defconfig with the same mechanism.

@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from a18de65 to a29a1e3 Compare June 9, 2026 03:09
@doronbehar

doronbehar commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @JamieMagee for investigating this. I managed to build the kernel without that -v# error with this diff for nixos-raspberrypi:

diff --git i/flake.lock w/flake.lock
index 8793b4b..b1142ed 100644
--- i/flake.lock
+++ w/flake.lock
@@ -57,16 +57,16 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1778737229,
-        "narHash": "sha256-6xWoytx8jFW4PF1GjRm/i/53trbpKGfz6zjzQGBr4cI=",
-        "owner": "NixOS",
+        "lastModified": 1780997208,
+        "narHash": "sha256-R3jeIi/5wjWJQItYcxp1E7reRjRBHCxdn7IWZZ29Ero=",
+        "owner": "doronbehar",
         "repo": "nixpkgs",
-        "rev": "d7a713c0b7e47c908258e71cba7a2d77cc8d71d5",
+        "rev": "4386af9eadecc12cd47673b881c8a8ffa154263c",
         "type": "github"
       },
       "original": {
-        "owner": "NixOS",
-        "ref": "nixos-25.11",
+        "owner": "doronbehar",
+        "ref": "tmp/jamieMagee-kernel-config@master",
         "repo": "nixpkgs",
         "type": "github"
       }
diff --git i/flake.nix w/flake.nix
index 0e10e90..3e087cd 100644
--- i/flake.nix
+++ w/flake.nix
@@ -1,18 +1,18 @@
 {
   description = "Flake for RaspberryPi support on NixOS";
 
-  nixConfig = {
-    extra-substituters = [
-      "https://nixos-raspberrypi.cachix.org"
-    ];
-    extra-trusted-public-keys = [
-      "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
-    ];
-    connect-timeout = 5;
-  };
+  #nixConfig = {
+  #  extra-substituters = [
+  #    "https://nixos-raspberrypi.cachix.org"
+  #  ];
+  #  extra-trusted-public-keys = [
+  #    "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
+  #  ];
+  #  connect-timeout = 5;
+  #};
 
   inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
+    nixpkgs.url = "github:doronbehar/nixpkgs/tmp/jamieMagee-kernel-config@master";
 
     argononed = {
       # url = "git+file:../argononed?shallow=1";
diff --git i/pkgs/linux-rpi/package.nix w/pkgs/linux-rpi/package.nix
index 1ce080e..afbbafb 100644
--- i/pkgs/linux-rpi/package.nix
+++ w/pkgs/linux-rpi/package.nix
@@ -25,7 +25,10 @@ let
             # specific to <name> and the arch it is being built for
             configFixup = args.fixupStructuredConfig.${name}.${arch} or { };
           in
-          configFixup // genericConfig;
+          configFixup // genericConfig // {
+            # Requires https://github.com/NixOS/nixpkgs/pull/517382
+            LOCALVERSION = lib.kernel.freeform "";
+          };
       };
     in
     {
@@ -98,12 +101,6 @@ in
   // (args.argsOverride or { })
 )).overrideAttrs
   {
-    postConfigure = ''
-      # The v7 defconfig has this set to '-v7' which screws up our modDirVersion.
-      sed -i $buildRoot/.config -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-      sed -i $buildRoot/include/config/auto.conf -e 's/^CONFIG_LOCALVERSION=.*/CONFIG_LOCALVERSION=""/'
-    '';
-
     postFixup =
       let
         armArch = if stdenv.isAarch64 then "arm64" else "arm";

While the diff to Nixpkgs master branch is (EDIT) this PR as is - the "simpler" substitution of $buildRoot/.config and/or $buildRoot/include/config/auto.conf is not working.

Could you please bring back that simpler substitution to .config file we discussed here? It was tested as in the above diffs.

@doronbehar

Copy link
Copy Markdown
Contributor

Wait I was wrong, the "simpler" substitution of $buildRoot/.config and/or $buildRoot/include/config/auto.conf is not working - my previous comment was false. You still were right as for the mkConfig diff in pkgs/libux-rpi/package.nix. I fully built the kernel with it.

I can only testify this works, but it'd be nice to get @alyssais' final approval on this PR - it should be fully ready now.

@nixpkgs-ci nixpkgs-ci Bot added the llm-assisted Contributions that include code generation with LLMs label Jul 19, 2026
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from a29a1e3 to ec28c4e Compare July 26, 2026 07:20
@nixpkgs-ci nixpkgs-ci Bot added the 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. label Jul 26, 2026
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from ec28c4e to 19d22cb Compare August 24, 2026 06:30
@nixpkgs-ci nixpkgs-ci Bot removed the 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. label Aug 24, 2026
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from 19d22cb to 2673bbe Compare August 24, 2026 06:36
@JamieMagee
JamieMagee requested a review from alyssais August 24, 2026 06:42

@doronbehar doronbehar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, Is this tested?

@JamieMagee

Copy link
Copy Markdown
Member Author

@doronbehar Yes. I reproduced the original nixos-raspberrypi case. I removed the old postConfigure sed workaround, set LOCALVERSION = lib.kernel.freeform "", and built the full linux_rpi3 kernel.

The generated config contains CONFIG_LOCALVERSION="" and the image reports Linux version 6.12.87.

@JamieMagee
JamieMagee requested a review from doronbehar August 25, 2026 00:10
@K900

K900 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Is the PR description just out of date now?

@JamieMagee

JamieMagee commented Aug 25, 2026 •

Copy link
Copy Markdown
Member Author

Yes, yes it is 🤦 Just a minute...

EDIT: Updated.

@doronbehar doronbehar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks better, but I will need to test it myself too.

@doronbehar

Copy link
Copy Markdown
Contributor

@JamieMagee what do you think about this alternative patch?

commit e70c26961740aaa05678c2781bce5f24661418c3
Author: Jamie Magee <[email protected]>
Date:   Wed May 6 09:30:12 2026 -0700

    linux/kernel: support empty-string values via lib.kernel.freeform ""
    
    Resolves #516936
    
    Assisted-by: GitHub Copilot (Claude Opus 4.8)

diff --git a/nixos/modules/system/boot/kernel_config.nix b/nixos/modules/system/boot/kernel_config.nix
index 115b55d98143..b5f9803f9daf 100644
--- a/nixos/modules/system/boot/kernel_config.nix
+++ b/nixos/modules/system/boot/kernel_config.nix
@@ -70,9 +70,7 @@ let
         ];
 
     in
-    if (val == "") then
-      "\"\""
-    else if val == "y" || val == "m" || val == "n" then
+    if val == "y" || val == "m" || val == "n" then
       val
     else if all isNumber (stringToCharacters val) then
       val
@@ -87,6 +85,9 @@ let
   #       VIRTIO_BLK y
   #       VIRTIO_CONSOLE n
   #       NET_9P_VIRTIO? y
+  #       LOCALVERSION
+  #
+  # A line without a value represents an empty string.
   #
   # Borrowed from copumpkin https://github.com/NixOS/nixpkgs/pull/12158
   # returns a string, expr should be an attribute set
@@ -100,9 +101,8 @@ let
         let
           val = if item.freeform != null then item.freeform else item.tristate;
         in
-        optionalString (val != null) (
-          if (item.optional) then "${key}? ${mkValue val}\n" else "${key} ${mkValue val}\n"
-        );
+        optionalString (val != null)
+          "${key}${optionalString item.optional "?"}${optionalString (val != "") " ${mkValue val}"}\n";
 
       mkConf = cfg: concatStrings (mapAttrsToList mkConfigLine cfg);
     in
@@ -119,6 +119,7 @@ in
       example = ''
         USB? y
         DEBUG n
+        LOCALVERSION
       '';
       description = ''
         The result of converting the structured kernel configuration in settings
diff --git a/pkgs/os-specific/linux/kernel/generate-config.pl b/pkgs/os-specific/linux/kernel/generate-config.pl
index 18d1f2150562..d1842623355a 100644
--- a/pkgs/os-specific/linux/kernel/generate-config.pl
+++ b/pkgs/os-specific/linux/kernel/generate-config.pl
@@ -4,10 +4,10 @@
 # and otherwise uses the default answer (as determined by the default
 # config for the architecture).  Overrides are read from the file
 # $KERNEL_CONFIG, which on each line contains an option name and an
-# answer, e.g. "EXT2_FS_POSIX_ACL y".  The script warns about ignored
-# options in $KERNEL_CONFIG, and barfs if `make config' selects
-# another answer for an option than the one provided in
-# $KERNEL_CONFIG.
+# answer, e.g. "EXT2_FS_POSIX_ACL y".  A name without an answer means
+# an empty string.  The script warns about ignored options in
+# $KERNEL_CONFIG, and barfs if `make config' selects another answer
+# for an option than the one provided in $KERNEL_CONFIG.
 
 use strict;
 use IPC::Open2;
@@ -22,15 +22,19 @@ my $buildRoot = $ENV{'BUILD_ROOT'};
 my $makeFlags = $ENV{'MAKE_FLAGS'};
 $SIG{PIPE} = 'IGNORE';
 
-# Read the answers.
+# Read the answers. A record without a value represents an empty string.
 my %answers;
 my %requiredAnswers;
 open ANSWERS, "<$ENV{KERNEL_CONFIG}" or die "Could not open answer file";
 while (<ANSWERS>) {
     chomp;
     s/#.*//;
-    if (/^\s*([A-Za-z0-9_]+)(\?)?\s+(.*\S)\s*$/) {
-        $answers{$1} = $3;
+    if (/^\s*([A-Za-z0-9_]+)(\?)?(?:\s+(.*\S))?\s*$/) {
+        if (defined $3) {
+            $answers{$1} = $3;
+        } else {
+            $answers{$1} = "";
+        }
         $requiredAnswers{$1} = !(defined $2);
     } elsif (!/^\s*$/) {
         die "invalid config line: $_";
@@ -140,20 +144,32 @@ runConfig;
 # Read the final .config file and check that our answers are in
 # there.  `make config' often overrides answers if later questions
 # cause options to be selected.
+#
+# While reading, also rewrite any option that was requested as an empty
+# string: `make config` treats an empty interactive answer as "use the
+# default", so a non-empty defconfig value (e.g. CONFIG_LOCALVERSION="-v8")
+# must be replaced directly in the file. Options missing from .config stay
+# absent, so the validation below still reports them as unused.
 my %config;
-open CONFIG, "<$buildRoot/.config" or die "Could not read .config";
-while (<CONFIG>) {
-    chomp;
-    if (/^CONFIG_([A-Za-z0-9_]+)="(.*)"$/) {
-        # String options have double quotes, e.g. 'CONFIG_NLS_DEFAULT="utf8"' and allow escaping.
-        ($config{$1} = $2) =~ s/\\([\\"])/$1/g;
-    } elsif (/^CONFIG_([A-Za-z0-9_]+)=(.*)$/) {
-        $config{$1} = $2;
-    } elsif (/^# CONFIG_([A-Za-z0-9_]+) is not set$/) {
-        $config{$1} = "n";
+{
+    local @ARGV = ("$buildRoot/.config");
+    local $^I = ""; # edit in place
+    while (<>) {
+        if (/^CONFIG_([^=]+)=/ && exists $answers{$1} && $answers{$1} eq "") {
+            $_ = "CONFIG_$1=\"\"\n";
+        }
+        chomp(my $line = $_);
+        if ($line =~ /^CONFIG_([A-Za-z0-9_]+)="(.*)"$/) {
+            # String options have double quotes, e.g. 'CONFIG_NLS_DEFAULT="utf8"' and allow escaping.
+            ($config{$1} = $2) =~ s/\\([\\"])/$1/g;
+        } elsif ($line =~ /^CONFIG_([A-Za-z0-9_]+)=(.*)$/) {
+            $config{$1} = $2;
+        } elsif ($line =~ /^# CONFIG_([A-Za-z0-9_]+) is not set$/) {
+            $config{$1} = "n";
+        }
+        print;
     }
 }
-close CONFIG;
 
 my $ret = 0;
 foreach my $name (sort (keys %answers)) {

I tested it and it works too, and I find it a bit more readable, and no extra variables are used, and .config is being written only once. I think that this is even more inline with what @alyssais requested in #517382 (comment) .

Resolves NixOS#516936

Assisted-by: GitHub Copilot (Claude Opus 4.8)
@JamieMagee
JamieMagee force-pushed the kernel-config/freeform-empty-string branch from 2673bbe to b7cd9bb Compare September 2, 2026 03:09
@JamieMagee

Copy link
Copy Markdown
Member Author

@doronbehar Taken, thanks. The merged pass does read better, and dropping %emptyStringAnswers is safe (the value group is (.*\S), so $answers{...} can only be empty when the line had no value at all).

I just made a few edits to the comments. Nothing major.

@doronbehar
doronbehar added this pull request to the merge queue Sep 2, 2026
Merged via the queue into NixOS:staging with commit 954478d Sep 2, 2026
24 checks passed
@JamieMagee
JamieMagee deleted the kernel-config/freeform-empty-string branch September 2, 2026 15:39
@emilazy

emilazy commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

FWIW I had the following patch locally waiting to be pushed out which handles this far more straightforwardly:

Commit ID: a4bccf21a6ac92d2d511f469e4ae38226cd4fec6
Change ID: pksuvuypqmuutzspvnlmqroxoxosnmzp
Author   : Emily <[email protected]> (2026-08-17 08:48:32)
Committer: Emily <[email protected]> (2026-09-08 23:10:11)

    buildLinux: support empty string configuration values

    This is meaningful for e.g. `SYSTEM_TRUSTED_KEYS`, and the upstream
    documentation has examples of using `scripts/config` to set an empty
    string in this way.

    Of course, driving the interactive configuration script this way is
    horrible in general and this whole thing should probably be rewritten,
    but this seems fine at a pinch.

diff --git a/nixos/modules/system/boot/kernel_config.nix b/nixos/modules/system/boot/kernel_config.nix
index 115b55d981..58b13ece06 100644
--- a/nixos/modules/system/boot/kernel_config.nix
+++ b/nixos/modules/system/boot/kernel_config.nix
@@ -70,9 +70,7 @@
         ];
 
     in
-    if (val == "") then
-      "\"\""
-    else if val == "y" || val == "m" || val == "n" then
+    if val == "y" || val == "m" || val == "n" then
       val
     else if all isNumber (stringToCharacters val) then
       val
diff --git a/pkgs/os-specific/linux/kernel/generate-config.pl b/pkgs/os-specific/linux/kernel/generate-config.pl
index 18d1f21505..bed4b9ca95 100644
--- a/pkgs/os-specific/linux/kernel/generate-config.pl
+++ b/pkgs/os-specific/linux/kernel/generate-config.pl
@@ -29,7 +29,7 @@
 while (<ANSWERS>) {
     chomp;
     s/#.*//;
-    if (/^\s*([A-Za-z0-9_]+)(\?)?\s+(.*\S)\s*$/) {
+    if (/^\s*([A-Za-z0-9_]+)(\?)?\s+(.*\S?)\s*$/) {
         $answers{$1} = $3;
         $requiredAnswers{$1} = !(defined $2);
     } elsif (!/^\s*$/) {
@@ -131,6 +131,18 @@
     waitpid $pid, 0;
 }
 
+# `make config` interprets an empty line as taking the previous/default
+# value of an option, so we need to set empty values ahead of time.
+foreach my $name (keys %answers) {
+    if ($answers{$name} eq "") {
+        system(
+            "$ENV{SRC}/scripts/config",
+            "--file", "$buildRoot/.config",
+            "--set-str", $name, "",
+        ) == 0 or die "Could not set empty $name";
+    }
+}
+
 # Run `make config' several times to converge on the desired result.
 # (Some options may only become available after other options are
 # set in a previous run.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: kernel The Linux kernel 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. llm-assisted Contributions that include code generation with LLMs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nixos/boot.kernelPatches: lib.kernel.freeform "" produces CONFIG_FOO="\"\"" instead of CONFIG_FOO=""

5 participants