From 9057b12058888857f5e817556e6774d526950984 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Thu, 10 Oct 2024 00:08:02 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9E=95=20move=20to=20`flake-parts`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flake.lock | 31 ++ flake.nix | 162 +--------- formatter.nix | 12 - nix/checks.nix | 16 + nix/crane.nix | 83 ++++++ nix/default.nix | 15 + nix/devshells.nix | 19 ++ nix/env.nix | 7 + nix/formatter.nix | 19 ++ nix/hmModules.nix | 11 + .../home-manager-module-legacy.nix | 0 nix/home-manager-module.nix | 277 ++++++++++++++++++ nix/lib.nix | 10 + nix/meta.nix | 9 + nix/packages.nix | 13 + 15 files changed, 512 insertions(+), 172 deletions(-) delete mode 100644 formatter.nix create mode 100644 nix/checks.nix create mode 100644 nix/crane.nix create mode 100644 nix/default.nix create mode 100644 nix/devshells.nix create mode 100644 nix/env.nix create mode 100644 nix/formatter.nix create mode 100644 nix/hmModules.nix rename home-manager-module.nix => nix/home-manager-module-legacy.nix (100%) create mode 100644 nix/home-manager-module.nix create mode 100644 nix/lib.nix create mode 100644 nix/meta.nix create mode 100644 nix/packages.nix diff --git a/flake.lock b/flake.lock index d98d88d..8a8192a 100644 --- a/flake.lock +++ b/flake.lock @@ -15,6 +15,24 @@ "type": "github" } }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1727826117, + "narHash": "sha256-K5ZLCyfO/Zj9mPFldf3iwS6oZStJcU4tSpiXTMYaaL0=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "3d04084d54bedc3d6b8b736c70ef449225c361b1", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "home-manager": { "inputs": { "nixpkgs": "nixpkgs" @@ -49,6 +67,18 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1727825735, + "narHash": "sha256-0xHYkMkeLVQAMa7gvkddbPqpxph+hDzdu1XdGPJR+Os=", + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz" + } + }, "nixpkgs_2": { "locked": { "lastModified": 1727296349, @@ -84,6 +114,7 @@ "root": { "inputs": { "crane": "crane", + "flake-parts": "flake-parts", "home-manager": "home-manager", "nixpkgs": "nixpkgs_2", "treefmt-nix": "treefmt-nix" diff --git a/flake.nix b/flake.nix index ddce9a5..f089c74 100644 --- a/flake.nix +++ b/flake.nix @@ -8,169 +8,11 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; home-manager.url = "github:nix-community/home-manager"; treefmt-nix.url = "github:numtide/treefmt-nix/"; crane.url = "github:ipetkov/crane"; }; - outputs = - { - self, - nixpkgs, - crane, - treefmt-nix, - home-manager, - ... - }: - let - system = "x86_64-linux"; - pkgs = import nixpkgs { inherit system; }; - - devInputs = with pkgs; [ - rustc - rustfmt - cargo - clippy - ]; - - nativeBuildInputs = with pkgs; [ - makeWrapper - # wifi plugin - # cargo networkmanager dependency - pkgconf - dbus - ]; - - buildInputs = with pkgs; [ dbus ]; - - cargoTOML = builtins.fromTOML (builtins.readFile (./. + "/Cargo.toml")); - - inherit (cargoTOML.workspace.package) version; - pname = "centerpiece"; - - craneLib = crane.mkLib nixpkgs.legacyPackages.${system}; - fontFilter = path: _type: builtins.match ".*ttf$" path != null; - configFilter = path: _type: builtins.match ".*config.yml$" path != null; - assetOrCargo = - path: type: - (configFilter path type) || (fontFilter path type) || (craneLib.filterCargoSources path type); - commonArgs = { - src = pkgs.lib.cleanSourceWith { - src = craneLib.path ./.; - filter = assetOrCargo; - }; - inherit - pname - version - buildInputs - nativeBuildInputs - ; - }; - cargoArtifacts = craneLib.buildDepsOnly commonArgs; - cargoClippy = craneLib.cargoClippy ( - commonArgs - // { - inherit cargoArtifacts; - cargoClippyExtraArgs = "--all-targets --all-features"; - } - ); - GIT_DATE = "${builtins.substring 0 4 self.lastModifiedDate}-${ - builtins.substring 4 2 self.lastModifiedDate - }-${builtins.substring 6 2 self.lastModifiedDate}"; - GIT_REV = self.shortRev or "Not committed yet."; - treefmt = (treefmt-nix.lib.evalModule pkgs ./formatter.nix).config.build; - libPath = pkgs.lib.makeLibraryPath [ - pkgs.wayland - pkgs.libxkbcommon - pkgs.vulkan-loader - pkgs.libGL - ]; - - in - { - devShells.${system}.default = pkgs.mkShell { - inherit - nativeBuildInputs - buildInputs - GIT_DATE - GIT_REV - ; - packages = devInputs ++ [ treefmt.wrapper ]; - LD_LIBRARY_PATH = libPath; - }; - packages.${system} = { - default = craneLib.buildPackage ( - commonArgs - // { - inherit - cargoArtifacts - nativeBuildInputs - buildInputs - pname - GIT_REV - GIT_DATE - ; - postFixup = pkgs.lib.optional pkgs.stdenv.isLinux '' - rpath=$(patchelf --print-rpath $out/bin/${pname}) - patchelf --set-rpath "$rpath:${libPath}" $out/bin/${pname} - ''; - - meta = with pkgs.lib; { - description = "Your trusty omnibox search."; - homepage = "https://github.com/friedow/centerpiece"; - platforms = platforms.linux; - license = licenses.mit; - mainProgram = pname; - maintainers = [ "friedow" ]; - }; - } - ); - index-git-repositories = craneLib.buildPackage ( - commonArgs - // rec { - inherit cargoArtifacts; - pname = "index-git-repositories"; - cargoExtraArgs = "-p ${pname}"; - meta.mainProgram = pname; - } - ); - }; - checks.${system} = { - inherit (self.outputs.packages.${system}) default index-git-repositories; - shell = self.outputs.devShells.${system}.default; - treefmt = treefmt.check self; - inherit cargoClippy; - hmModule = - (nixpkgs.lib.nixosSystem { - system = "x86_64-linux"; - modules = [ - home-manager.nixosModules.home-manager - { - home-manager.users.alice = { - imports = [ self.outputs.hmModules."x86_64-linux".default ]; - programs.centerpiece = { - enable = true; - config.plugin.git_repositories.commands = [ [ "alacritty" ] ]; - services.index-git-repositories = { - enable = true; - interval = "3hours"; - }; - }; - home.stateVersion = "23.11"; - }; - users.users.alice = { - isNormalUser = true; - uid = 1000; - home = "/home/alice"; - }; - } - ]; - }).config.system.build.vm; - }; - hmModules.${system}.default = import ./home-manager-module.nix { - centerpiece = self.outputs.packages.${system}.default; - inherit (self.outputs.packages.${system}) index-git-repositories; - }; - formatter.${system} = treefmt.wrapper; - }; + outputs = args: import ./nix args; } diff --git a/formatter.nix b/formatter.nix deleted file mode 100644 index 5a4a512..0000000 --- a/formatter.nix +++ /dev/null @@ -1,12 +0,0 @@ -_: { - projectRootFile = "flake.nix"; - - programs = { - deadnix.enable = true; - nixfmt.enable = true; - rustfmt.enable = true; - statix.enable = true; - taplo.enable = true; - yamlfmt.enable = true; - }; -} diff --git a/nix/checks.nix b/nix/checks.nix new file mode 100644 index 0000000..595dcc2 --- /dev/null +++ b/nix/checks.nix @@ -0,0 +1,16 @@ +{ self, ... }: +{ + perSystem = + { pkgs, ... }: + { + checks = { + inherit ((pkgs.callPackage ./crane.nix { inherit self; })) + centerpiece + index-git-repositories + cargoArtifacts + cargoClippy + cargoTest + ; + }; + }; +} diff --git a/nix/crane.nix b/nix/crane.nix new file mode 100644 index 0000000..d0762f0 --- /dev/null +++ b/nix/crane.nix @@ -0,0 +1,83 @@ +{ + self, + lib, + pkgs, + makeWrapper, + pkgconf, + dbus, +}: +let + cargoTOML = builtins.fromTOML (builtins.readFile (self + "/Cargo.toml")); + inherit (cargoTOML.workspace.package) version; + pname = "centerpiece"; + meta = import ./meta.nix { inherit lib; }; + craneLib = self.inputs.crane.mkLib pkgs; + commonArgs = { + nativeBuildInputs = [ + makeWrapper + # wifi plugin + # cargo networkmanager dependency + pkgconf + dbus + ]; + buildInputs = [ dbus ]; + inherit + meta + version + pname + ; + src = + let + fontFilter = path: _type: builtins.match ".*ttf$" path != null; + configFilter = path: _type: builtins.match ".*config.yml$" path != null; + assetOrCargo = + path: type: + (configFilter path type) || (fontFilter path type) || (craneLib.filterCargoSources path type); + in + lib.cleanSourceWith { + src = craneLib.path ../.; + filter = assetOrCargo; + }; + }; + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + cargoClippy = craneLib.cargoClippy ( + commonArgs + // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets --all-features"; + } + ); + cargoTest = craneLib.cargoNextest (commonArgs // { inherit cargoArtifacts; }); +in +{ + centerpiece = craneLib.buildPackage ( + commonArgs + // { + env = import ./env.nix { inherit self; }; + postFixup = pkgs.lib.optional pkgs.stdenv.isLinux '' + rpath=$(patchelf --print-rpath $out/bin/${pname}) + patchelf --set-rpath "$rpath:${pkgs.callPackage ./lib.nix { }}" $out/bin/${pname} + ''; + doCheck = false; + inherit + cargoArtifacts + ; + } + ); + index-git-repositories = craneLib.buildPackage ( + commonArgs + // { + cargoExtraArgs = "-p index-git-repositories"; + pname = "index-git-repositories"; + doCheck = false; + inherit + cargoArtifacts + ; + } + ); + inherit + cargoClippy + cargoArtifacts + cargoTest + ; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..5b6c3be --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,15 @@ +{ flake-parts, ... }@inputs: +flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ + "x86_64-linux" + "aarch64-linux" + ]; + + imports = [ + ./devshells.nix + ./formatter.nix + ./packages.nix + ./checks.nix + ./hmModules.nix + ]; +} diff --git a/nix/devshells.nix b/nix/devshells.nix new file mode 100644 index 0000000..7140936 --- /dev/null +++ b/nix/devshells.nix @@ -0,0 +1,19 @@ +{ self, ... }: +{ + perSystem = + { pkgs, self', ... }: + { + devShells = { + default = pkgs.mkShellNoCC { + name = "centerpiece"; + inputsFrom = [ self'.packages.default ]; + packages = [ + pkgs.clippy + self'.formatter.outPath + ]; + env = import ./env.nix { inherit self; }; + LD_LIBRARY_PATH = pkgs.callPackage ./lib.nix { }; + }; + }; + }; +} diff --git a/nix/env.nix b/nix/env.nix new file mode 100644 index 0000000..e40403b --- /dev/null +++ b/nix/env.nix @@ -0,0 +1,7 @@ +{ self, ... }: +{ + GIT_DATE = "${builtins.substring 0 4 self.lastModifiedDate}-${ + builtins.substring 4 2 self.lastModifiedDate + }-${builtins.substring 6 2 self.lastModifiedDate}"; + GIT_REV = self.shortRev or self.dirtyShortRev; +} diff --git a/nix/formatter.nix b/nix/formatter.nix new file mode 100644 index 0000000..134c063 --- /dev/null +++ b/nix/formatter.nix @@ -0,0 +1,19 @@ +{ inputs, ... }: +{ + imports = [ inputs.treefmt-nix.flakeModule ]; + + perSystem = _: { + treefmt = { + projectRootFile = ".git/config"; + + programs = { + deadnix.enable = true; + nixfmt.enable = true; + rustfmt.enable = true; + statix.enable = true; + taplo.enable = true; + yamlfmt.enable = true; + }; + }; + }; +} diff --git a/nix/hmModules.nix b/nix/hmModules.nix new file mode 100644 index 0000000..2284a1c --- /dev/null +++ b/nix/hmModules.nix @@ -0,0 +1,11 @@ +self: +let + system = "x86_64-linux"; +in +{ + flake.hmModules."${system}".default = import ./home-manager-module-legacy.nix { + centerpiece = self.outputs.packages.${system}.default; + inherit (self.outputs.packages.${system}) index-git-repositories; + }; + flake.hmModules.default = import ./home-manager-module.nix self; +} diff --git a/home-manager-module.nix b/nix/home-manager-module-legacy.nix similarity index 100% rename from home-manager-module.nix rename to nix/home-manager-module-legacy.nix diff --git a/nix/home-manager-module.nix b/nix/home-manager-module.nix new file mode 100644 index 0000000..48a8d5d --- /dev/null +++ b/nix/home-manager-module.nix @@ -0,0 +1,277 @@ +self: +{ + lib, + pkgs, + config, + ... +}: +let + cfg = config.programs.centerpiece; + git-index-name = "index-git-repositories"; +in +{ + options.programs.centerpiece = { + enable = lib.mkEnableOption (lib.mdDoc "Centerpiece"); + + config = { + color = { + text = lib.mkOption { + default = "#ffffff"; + type = lib.types.str; + description = lib.mdDoc "Text color within centerpiece."; + }; + + background = lib.mkOption { + default = "#000000"; + type = lib.types.str; + description = lib.mdDoc "Background color within centerpiece."; + }; + }; + + plugin = { + applications = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + brave_bookmarks = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + brave_history = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + brave_progressive_web_apps = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + clock = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + firefox_bookmarks = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + firefox_history = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + git_repositories = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + zoxide = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable zoxide integration."; + }; + commands = lib.mkOption { + default = [ + [ + "alacritty" + "--command" + "nvim" + "$GIT_DIRECTORY" + ] + [ + "alacritty" + "--working-directory" + "$GIT_DIRECTORY" + ] + ]; + type = lib.types.listOf (lib.types.listOf lib.types.str); + description = lib.mdDoc '' + The commands to launch when an entry is selected. + Use the $GIT_DIRECTORY variable to pass in the selected directory. + Use the $GIT_DIRECTORY_NAME variable to pass in the selected directory name. + ''; + example = [ + [ + "code" + "--new-window" + "$GIT_DIRECTORY" + ] + [ + "alacritty" + "--command" + "lazygit" + "--path" + "$GIT_DIRECTORY" + ] + [ + "alacritty" + "--working-directory" + "$GIT_DIRECTORY" + ] + ]; + }; + }; + + gitmoji = { + enable = lib.mkOption { + default = false; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + resource_monitor_battery = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + resource_monitor_cpu = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + resource_monitor_disks = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + resource_monitor_memory = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + sway_windows = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + system = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + + wifi = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the plugin."; + }; + }; + }; + }; + + services.index-git-repositories = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + description = lib.mdDoc "Enable / disable the git repositories indexer service."; + }; + interval = lib.mkOption { + default = "5min"; + type = lib.types.str; + example = "hourly"; + description = lib.mdDoc '' + Frequency of index creation. + + The format is described in + {manpage}`systemd.time(7)`. + ''; + }; + }; + }; + + config = lib.mkMerge [ + (lib.mkIf cfg.enable { + home.packages = [ + self.packages.${pkgs.stdenv.system}.centerpiece + ]; + }) + + (lib.mkIf cfg.enable { + home.file.".config/centerpiece/config.yml".text = lib.generators.toYAML { } cfg.config; + }) + + (lib.mkIf cfg.services.index-git-repositories.enable { + systemd.user = { + services = { + index-git-repositories-service = { + Unit = { + Description = "Centerpiece - your trusty omnibox search"; + Documentation = "https://github.com/friedow/centerpiece"; + }; + + Service = { + ExecStart = "${pkgs.writeShellScript "${git-index-name}-service-ExecStart" '' + exec ${lib.getExe self.packages.${pkgs.stdenv.system}.index-git-repositories} + ''}"; + Type = "oneshot"; + Nice = 19; + IOSchedulingClass = "best-effort"; + IOSchedulingPriority = 7; + }; + }; + }; + timers = { + index-git-repositories-timer = { + Unit = { + Description = "Activate the git repository indexer"; + }; + Install = { + WantedBy = [ "timers.target" ]; + }; + Timer = { + OnUnitActiveSec = cfg.services.index-git-repositories.interval; + OnBootSec = "0min"; + Persistent = true; + Unit = "${git-index-name}-service.service"; + }; + }; + }; + }; + }) + ]; +} diff --git a/nix/lib.nix b/nix/lib.nix new file mode 100644 index 0000000..a2e62df --- /dev/null +++ b/nix/lib.nix @@ -0,0 +1,10 @@ +{ + pkgs, + ... +}: +pkgs.lib.makeLibraryPath [ + pkgs.wayland + pkgs.libxkbcommon + pkgs.vulkan-loader + pkgs.libGL +] diff --git a/nix/meta.nix b/nix/meta.nix new file mode 100644 index 0000000..96a31cb --- /dev/null +++ b/nix/meta.nix @@ -0,0 +1,9 @@ +{ lib, ... }: +{ + description = "Your trusty omnibox search."; + homepage = "https://github.com/friedow/centerpiece"; + mainProgram = "centerpiece"; + platforms = lib.platforms.linux; + license = [ lib.licenses.mit ]; + maintainers = [ "friedow" ]; +} diff --git a/nix/packages.nix b/nix/packages.nix new file mode 100644 index 0000000..d24ed8c --- /dev/null +++ b/nix/packages.nix @@ -0,0 +1,13 @@ +_: { + perSystem = + { self', ... }: + { + packages = rec { + default = centerpiece; + inherit (self'.checks) + centerpiece + index-git-repositories + ; + }; + }; +} From 580ad29c63da020a8eec8a0c14a1f831e637a4ca Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 21 Jan 2025 22:03:11 +0100 Subject: [PATCH 2/3] remove outputs --- nix/hmModules.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix/hmModules.nix b/nix/hmModules.nix index 2284a1c..3ed6d69 100644 --- a/nix/hmModules.nix +++ b/nix/hmModules.nix @@ -4,8 +4,8 @@ let in { flake.hmModules."${system}".default = import ./home-manager-module-legacy.nix { - centerpiece = self.outputs.packages.${system}.default; - inherit (self.outputs.packages.${system}) index-git-repositories; + centerpiece = self.packages.${system}.default; + inherit (self.packages.${system}) index-git-repositories; }; flake.hmModules.default = import ./home-manager-module.nix self; } From 76c232592f1cc3f393f2c6617d6a98c90be335df Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 21 Jan 2025 22:36:35 +0100 Subject: [PATCH 3/3] WIP --- nix/hmModules.nix | 8 ++++---- nix/home-manager-module-legacy.nix | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/nix/hmModules.nix b/nix/hmModules.nix index 3ed6d69..6aad78a 100644 --- a/nix/hmModules.nix +++ b/nix/hmModules.nix @@ -1,11 +1,11 @@ -self: +inputs: let system = "x86_64-linux"; in { flake.hmModules."${system}".default = import ./home-manager-module-legacy.nix { - centerpiece = self.packages.${system}.default; - inherit (self.packages.${system}) index-git-repositories; + centerpiece = inputs.self.outputs.packages.${system}.default; + inherit (inputs.self.outputs.packages.${system}) index-git-repositories; }; - flake.hmModules.default = import ./home-manager-module.nix self; + flake.hmModules.default = import ./home-manager-module.nix inputs.self; } diff --git a/nix/home-manager-module-legacy.nix b/nix/home-manager-module-legacy.nix index 52f6ad1..43c6349 100644 --- a/nix/home-manager-module-legacy.nix +++ b/nix/home-manager-module-legacy.nix @@ -14,6 +14,7 @@ in enable = lib.mkEnableOption (lib.mdDoc "Centerpiece"); config = { + color = { text = lib.mkOption { default = "#ffffff"; @@ -225,6 +226,7 @@ in }; config = lib.mkMerge [ + {warnings = ["This home-manager module is deprecated."];} (lib.mkIf cfg.enable { home.packages = [ centerpiece ]; }) (lib.mkIf cfg.enable {