From 1de7b377f411b920b22f050b28ab67d0203b82c4 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:34:28 -0600
Subject: [PATCH 01/10] rl-2405: change note for bcachefs to be
 'linuxPackages_latest'

---
 nixos/doc/manual/release-notes/rl-2405.section.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nixos/doc/manual/release-notes/rl-2405.section.md b/nixos/doc/manual/release-notes/rl-2405.section.md
index c075149a0e5d5..5c7b169ff8da8 100644
--- a/nixos/doc/manual/release-notes/rl-2405.section.md
+++ b/nixos/doc/manual/release-notes/rl-2405.section.md
@@ -10,7 +10,7 @@ In addition to numerous new and upgraded packages, this release has the followin
 
 - `screen`'s module has been cleaned, and will now require you to set `programs.screen.enable` in order to populate `screenrc` and add the program to the environment.
 
-- `linuxPackages_testing_bcachefs` is now fully deprecated by `linuxPackages_testing`, and is therefore no longer available.
+- `linuxPackages_testing_bcachefs` is now fully deprecated by `linuxPackages_latest`, and is therefore no longer available.
 
 - NixOS now installs a stub ELF loader that prints an informative error message when users attempt to run binaries not made for NixOS.
    - This can be disabled through the `environment.stub-ld.enable` option.

From 919c05460b6711f5d8beb6c1e2fff91b0b831a56 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:38:16 -0600
Subject: [PATCH 02/10] nixos/stage-1.init.sh: fix IFS comment grammar

---
 nixos/modules/system/boot/stage-1-init.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/nixos/modules/system/boot/stage-1-init.sh b/nixos/modules/system/boot/stage-1-init.sh
index 086e5d65da2f2..72c670bcaed01 100644
--- a/nixos/modules/system/boot/stage-1-init.sh
+++ b/nixos/modules/system/boot/stage-1-init.sh
@@ -86,8 +86,8 @@ touch /etc/initrd-release
 # Function for waiting for device(s) to appear.
 waitDevice() {
     local device="$1"
-    # Split device string using ':' as a delimiter as bcachefs
-    # uses this for multi-device filesystems, i.e. /dev/sda1:/dev/sda2:/dev/sda3
+    # Split device string using ':' as a delimiter, bcachefs uses
+    # this for multi-device filesystems, i.e. /dev/sda1:/dev/sda2:/dev/sda3
     local IFS=':'
 
     # USB storage devices tend to appear with some delay.  It would be

From d86395db78fccd6c0d047ac987e05c3a9de2dcf7 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:46:30 -0600
Subject: [PATCH 03/10] nixos/stage-1.init.sh: only set 'IFS' in 'waitDevice'
 for bcachefs

---
 nixos/modules/system/boot/stage-1-init.sh | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/nixos/modules/system/boot/stage-1-init.sh b/nixos/modules/system/boot/stage-1-init.sh
index 72c670bcaed01..59cf1a47fb7f9 100644
--- a/nixos/modules/system/boot/stage-1-init.sh
+++ b/nixos/modules/system/boot/stage-1-init.sh
@@ -88,7 +88,12 @@ waitDevice() {
     local device="$1"
     # Split device string using ':' as a delimiter, bcachefs uses
     # this for multi-device filesystems, i.e. /dev/sda1:/dev/sda2:/dev/sda3
-    local IFS=':'
+    local IFS
+
+    # bcachefs is the only known use for this at the moment
+    # Preferably, the 'UUID=' syntax should be enforced, but
+    # this is kept for compatibility reasons
+    if [ "$fsType" = bcachefs ]; then IFS=':'; fi
 
     # USB storage devices tend to appear with some delay.  It would be
     # great if we had a way to synchronously wait for them, but

From 9dfa878a20648152ef7851d3bf863a54fb52ffdd Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:50:44 -0600
Subject: [PATCH 04/10] nixos/bcachefs: clarify 'FIXME' comment on when to
 remove

---
 nixos/modules/tasks/filesystems/bcachefs.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nixos/modules/tasks/filesystems/bcachefs.nix b/nixos/modules/tasks/filesystems/bcachefs.nix
index fdb149a3d9a17..c696ddfc24f60 100644
--- a/nixos/modules/tasks/filesystems/bcachefs.nix
+++ b/nixos/modules/tasks/filesystems/bcachefs.nix
@@ -123,7 +123,7 @@ in
       inherit assertions;
       # needed for systemd-remount-fs
       system.fsPackages = [ pkgs.bcachefs-tools ];
-      # FIXME: Remove this line when the default kernel has bcachefs
+      # FIXME: Remove this line when the LTS (default) kernel is at least version 6.7
       boot.kernelPackages = lib.mkDefault pkgs.linuxPackages_latest;
       systemd.services = lib.mapAttrs' (mkUnits "") (lib.filterAttrs (n: fs: (fs.fsType == "bcachefs") && (!utils.fsNeededForBoot fs)) config.fileSystems);
     }

From 37615c68b436106e27ea6f91242c39ec0f8d0082 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:53:40 -0600
Subject: [PATCH 05/10] kernel/common-config: add note on when to remove
 conditional and make default yes for bcachefs

---
 pkgs/os-specific/linux/kernel/common-config.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/os-specific/linux/kernel/common-config.nix b/pkgs/os-specific/linux/kernel/common-config.nix
index 2aacb88dc87e0..ecab205f989a7 100644
--- a/pkgs/os-specific/linux/kernel/common-config.nix
+++ b/pkgs/os-specific/linux/kernel/common-config.nix
@@ -478,6 +478,7 @@ let
 
       BTRFS_FS_POSIX_ACL = yes;
 
+      # FIXME: Make these by default 'yes' when LTS kernel (default) is at least version 6.7
       BCACHEFS_QUOTA = whenAtLeast "6.7" (option yes);
       BCACHEFS_POSIX_ACL = whenAtLeast "6.7" (option yes);
 

From b1ce631c29da99e29d4959b8e7e3e83ad91ef2e6 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 07:57:36 -0600
Subject: [PATCH 06/10] aliases: change 'linux(Packages)_testing_bcachefs'
 notes to use 'linux(Packages)_latest' or any kernel version at least 6.7

---
 pkgs/top-level/aliases.nix | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/pkgs/top-level/aliases.nix b/pkgs/top-level/aliases.nix
index eca32582217b6..f006446bf9d7c 100644
--- a/pkgs/top-level/aliases.nix
+++ b/pkgs/top-level/aliases.nix
@@ -602,9 +602,9 @@ mapAliases ({
   '';
   linux_latest_hardened = linuxPackages_latest_hardened;
 
-  # Added 2023-11-18
-  linuxPackages_testing_bcachefs = throw "'linuxPackages_testing_bcachefs' has been removed, please use 'linuxPackages_testing', or any other linux kernel with bcachefs support";
-  linux_testing_bcachefs = throw "'linux_testing_bcachefs' has been removed, please use 'linux_testing', or any other linux kernel with bcachefs support";
+  # Added 2023-11-18, modified 2024-01-09
+  linuxPackages_testing_bcachefs = throw "'linuxPackages_testing_bcachefs' has been removed, please use 'linuxPackages_latest', any kernel version at least 6.7, or any other linux kernel with bcachefs support";
+  linux_testing_bcachefs = throw "'linux_testing_bcachefs' has been removed, please use 'linux_latest', any kernel version at least 6.7, or any other linux kernel with bcachefs support";
 
   lld_6 = throw "lld_6 has been removed from nixpkgs"; # Added 2024-01-08
   lld_7 = throw "lld_7 has been removed from nixpkgs"; # Added 2023-11-19

From 6b8dc42ee1218515c17a8527e659368e58e7da39 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 09:09:02 -0600
Subject: [PATCH 07/10] bcachefs-tools: fixup for version 1.4.1

---
 .../tools/filesystems/bcachefs-tools/default.nix | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/pkgs/tools/filesystems/bcachefs-tools/default.nix b/pkgs/tools/filesystems/bcachefs-tools/default.nix
index 17b438ac46b88..31d46d0989783 100644
--- a/pkgs/tools/filesystems/bcachefs-tools/default.nix
+++ b/pkgs/tools/filesystems/bcachefs-tools/default.nix
@@ -19,6 +19,7 @@
 , rustPlatform
 , makeWrapper
 , writeScript
+, python3
 , fuseSupport ? false
 }:
 
@@ -26,7 +27,6 @@ stdenv.mkDerivation (finalAttrs: {
   pname = "bcachefs-tools";
   version = "1.4.1";
 
-
   src = fetchFromGitHub {
     owner = "koverstreet";
     repo = "bcachefs-tools";
@@ -69,8 +69,7 @@ stdenv.mkDerivation (finalAttrs: {
   checkFlags = [ "BCACHEFS_TEST_USE_VALGRIND=no" ];
 
   makeFlags = [
-    "DESTDIR=${placeholder "out"}"
-    "PREFIX="
+    "PREFIX=${placeholder "out"}"
     "VERSION=${finalAttrs.version}"
     "INITRAMFS_DIR=${placeholder "out"}/etc/initramfs-tools"
   ];
@@ -79,6 +78,17 @@ stdenv.mkDerivation (finalAttrs: {
     rm tests/test_fuse.py
   '';
 
+  # Tries to install to the 'systemd-minimal' and 'udev' nix installation paths
+  installFlags = [
+    "PKGCONFIG_SERVICEDIR=$(out)/lib/systemd/system"
+    "PKGCONFIG_UDEVDIR=$(out)/lib/udev"
+  ];
+
+  postInstall = ''
+    substituteInPlace $out/libexec/bcachefsck_all \
+      --replace "/usr/bin/python3" "${python3}/bin/python3"
+  '';
+
   passthru = {
     tests = {
       smoke-test = nixosTests.bcachefs;

From c3316bcce4ba7ead40a04b2c7aa9c6aeb33c4a03 Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Tue, 9 Jan 2024 09:09:32 -0600
Subject: [PATCH 08/10] nixos/bcachefs: add 'bcachefs-tools' to
 (udev/systemd).packages

---
 nixos/modules/tasks/filesystems/bcachefs.nix | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/nixos/modules/tasks/filesystems/bcachefs.nix b/nixos/modules/tasks/filesystems/bcachefs.nix
index c696ddfc24f60..3b990ce30b21e 100644
--- a/nixos/modules/tasks/filesystems/bcachefs.nix
+++ b/nixos/modules/tasks/filesystems/bcachefs.nix
@@ -125,7 +125,12 @@ in
       system.fsPackages = [ pkgs.bcachefs-tools ];
       # FIXME: Remove this line when the LTS (default) kernel is at least version 6.7
       boot.kernelPackages = lib.mkDefault pkgs.linuxPackages_latest;
-      systemd.services = lib.mapAttrs' (mkUnits "") (lib.filterAttrs (n: fs: (fs.fsType == "bcachefs") && (!utils.fsNeededForBoot fs)) config.fileSystems);
+      services.udev.packages = [ pkgs.bcachefs-tools ];
+
+      systemd = {
+        packages = [ pkgs.bcachefs-tools ];
+        services = lib.mapAttrs' (mkUnits "") (lib.filterAttrs (n: fs: (fs.fsType == "bcachefs") && (!utils.fsNeededForBoot fs)) config.fileSystems);
+      };
     }
 
     (lib.mkIf ((lib.elem "bcachefs" config.boot.initrd.supportedFilesystems) || (bootFs != {})) {

From 5a06c7fc22d66e3ba031ab42f1dae04a418f6cda Mon Sep 17 00:00:00 2001
From: Madoura <madouura@gmail.com>
Date: Sun, 14 Jan 2024 18:19:22 -0600
Subject: [PATCH 09/10] bcachefs-tools: add comment on when to enable build
 tests

---
 pkgs/tools/filesystems/bcachefs-tools/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/tools/filesystems/bcachefs-tools/default.nix b/pkgs/tools/filesystems/bcachefs-tools/default.nix
index 31d46d0989783..f45e4b51953f7 100644
--- a/pkgs/tools/filesystems/bcachefs-tools/default.nix
+++ b/pkgs/tools/filesystems/bcachefs-tools/default.nix
@@ -65,6 +65,7 @@ stdenv.mkDerivation (finalAttrs: {
     udev
   ] ++ lib.optional fuseSupport fuse3;
 
+  # FIXME: Try enabling this once the default linux kernel is at least 6.7
   doCheck = false; # needs bcachefs module loaded on builder
   checkFlags = [ "BCACHEFS_TEST_USE_VALGRIND=no" ];
 

From 874c91834c0be39e7e9dbb366d2a1d309a4f928c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <Mic92@users.noreply.github.com>
Date: Wed, 17 Jan 2024 08:59:05 +0100
Subject: [PATCH 10/10] linux: drop redundant bcachefs option hint

---
 pkgs/os-specific/linux/kernel/common-config.nix | 1 -
 1 file changed, 1 deletion(-)

diff --git a/pkgs/os-specific/linux/kernel/common-config.nix b/pkgs/os-specific/linux/kernel/common-config.nix
index ecab205f989a7..2aacb88dc87e0 100644
--- a/pkgs/os-specific/linux/kernel/common-config.nix
+++ b/pkgs/os-specific/linux/kernel/common-config.nix
@@ -478,7 +478,6 @@ let
 
       BTRFS_FS_POSIX_ACL = yes;
 
-      # FIXME: Make these by default 'yes' when LTS kernel (default) is at least version 6.7
       BCACHEFS_QUOTA = whenAtLeast "6.7" (option yes);
       BCACHEFS_POSIX_ACL = whenAtLeast "6.7" (option yes);