From 82b5c755d6ae46e75788811ca093089a4f984070 Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Sun, 15 Mar 2026 03:35:37 +0100 Subject: [PATCH] auto cleaning of nix store using nix-sweep --- flake.lock | 54 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 4 ++++ lib/mkNixosSystem.nix | 2 ++ modules/system/nix.nix | 19 ++++++++++++++- 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/flake.lock b/flake.lock index 10b57ab..f797330 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "cf": { + "locked": { + "lastModified": 1756852014, + "narHash": "sha256-OrfTRBwerFrlK1fbWhu1eW40XUrOdE/Rq7IyxthEpcA=", + "owner": "jzbor", + "repo": "cornflakes", + "rev": "7ef6d8be9b66f46cee3af1d5797e01f301b93149", + "type": "github" + }, + "original": { + "owner": "jzbor", + "repo": "cornflakes", + "type": "github" + } + }, "claude-code": { "inputs": { "flake-utils": "flake-utils", @@ -37,6 +52,21 @@ "type": "github" } }, + "crane_2": { + "locked": { + "lastModified": 1762538466, + "narHash": "sha256-8zrIPl6J+wLm9MH5ksHcW7BUHo7jSNOu0/hA0ohOOaM=", + "owner": "ipetkov", + "repo": "crane", + "rev": "0cea393fffb39575c46b7a0318386467272182fe", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -154,6 +184,29 @@ "type": "github" } }, + "nix-sweep": { + "inputs": { + "cf": "cf", + "crane": "crane_2", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1772493505, + "narHash": "sha256-4EWS5n9wWSNEUuqj8Nh3sPCjjRSOEh4bUd7j+wqDpSA=", + "owner": "jzbor", + "repo": "nix-sweep", + "rev": "6fed48ccc7afdca7f8ea1b20260bcf736aad9ee2", + "type": "github" + }, + "original": { + "owner": "jzbor", + "ref": "main", + "repo": "nix-sweep", + "type": "github" + } + }, "nixos-hardware": { "locked": { "lastModified": 1772972630, @@ -238,6 +291,7 @@ "home-manager": "home-manager", "lanzaboote": "lanzaboote", "nix-flatpak": "nix-flatpak", + "nix-sweep": "nix-sweep", "nixos-hardware": "nixos-hardware", "nixpkgs": "nixpkgs", "plasma-manager": "plasma-manager" diff --git a/flake.nix b/flake.nix index 2f91758..749edea 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,10 @@ url = "github:sadjow/claude-code-nix/main"; inputs.nixpkgs.follows = "nixpkgs"; }; + nix-sweep = { + url = "github:jzbor/nix-sweep/main"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = { self, nixos-hardware, ... }@inputs: diff --git a/lib/mkNixosSystem.nix b/lib/mkNixosSystem.nix index 9f0e311..a349f11 100644 --- a/lib/mkNixosSystem.nix +++ b/lib/mkNixosSystem.nix @@ -6,6 +6,7 @@ plasma-manager, lanzaboote, claude-code, + nix-sweep, ... }: hardwareConfig: hostConfig: @@ -27,6 +28,7 @@ nixpkgs.lib.nixosSystem { lanzaboote.nixosModules.lanzaboote hardwareConfig home-manager.nixosModules.home-manager + nix-sweep.nixosModules.default ../modules hostConfig ]; diff --git a/modules/system/nix.nix b/modules/system/nix.nix index f815117..7f08a8a 100644 --- a/modules/system/nix.nix +++ b/modules/system/nix.nix @@ -1,10 +1,27 @@ { config, lib, pkgs, modulesPath, ... }: +let + keepGenerations = if config.boot.lanzaboote.enable then + config.boot.lanzaboote.configurationLimit + else if config.boot.loader.systemd-boot.enable then + config.boot.loader.systemd-boot.configurationLimit + else null; +in { nix = { daemonIOSchedClass = "idle"; daemonCPUSchedPolicy = "idle"; settings.trusted-users = [ "root" "user" ]; - gc.automatic = true; + }; + + # Clean up nix store from old configurations usinx nix-sweep + services.nix-sweep = { + enable = true; + + # Automatically determine configuration limit from bootloader + keepMax = keepGenerations; + keepMin = if keepGenerations != null then keepGenerations else 10; + + gc = true; # Run GC afterwards }; } \ No newline at end of file