From ffab35d8fb65a7ce3f8059c0fe653a5fecbbbab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Fri, 12 Apr 2024 12:51:08 +0200 Subject: [PATCH] Create home-manager module for packer --- flake.lock | 40 ++------------------ flake.nix | 39 +++++++++---------- hm.module.nix | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.nix | 21 +++++++++++ 4 files changed, 142 insertions(+), 59 deletions(-) create mode 100644 hm.module.nix create mode 100644 package.nix diff --git a/flake.lock b/flake.lock index c3e7d21..ead1d37 100644 --- a/flake.lock +++ b/flake.lock @@ -77,24 +77,6 @@ "type": "indirect" } }, - "flake-utils": { - "inputs": { - "systems": "systems_2" - }, - "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "flake-utils", - "type": "github" - } - }, "nix-fetcher-data": { "inputs": { "flake-part": "flake-part", @@ -172,11 +154,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1712608508, - "narHash": "sha256-vMZ5603yU0wxgyQeHJryOI+O61yrX2AHwY6LOFyV1gM=", + "lastModified": 1712791164, + "narHash": "sha256-3sbWO1mbpWsLepZGbWaMovSO7ndZeFqDSdX0hZ9nVyw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "4cba8b53da471aea2ab2b0c1f30a81e7c451f4b6", + "rev": "1042fd8b148a9105f3c0aca3a6177fd1d9360ba5", "type": "github" }, "original": { @@ -189,7 +171,6 @@ "root": { "inputs": { "clj-nix": "clj-nix", - "flake-utils": "flake-utils", "nixpkgs": "nixpkgs_2" } }, @@ -207,21 +188,6 @@ "repo": "default", "type": "github" } - }, - "systems_2": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 30bdda1..05c0556 100644 --- a/flake.nix +++ b/flake.nix @@ -1,36 +1,31 @@ { - description = "A clj-nix flake"; + description = "Packer - Clojure application which boosts the luggage packing activity."; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - flake-utils.url = "github:numtide/flake-utils"; clj-nix.url = "github:jlesquembre/clj-nix"; }; outputs = { self, nixpkgs, - flake-utils, clj-nix, - }: - flake-utils.lib.eachDefaultSystem (system: { - packages = { - default = clj-nix.lib.mkCljApp { - pkgs = nixpkgs.legacyPackages.${system}; - modules = [ - # Option list: - # https://jlesquembre.github.io/clj-nix/options/ - { - projectSrc = ./.; - name = "packer"; - main-ns = "packer.main"; + }: let + systems = [ + "aarch64-linux" + "i686-linux" + "x86_64-linux" + "aarch64-darwin" + "x86_64-darwin" + ]; - nativeImage.enable = false; - - # customJdk.enable = true; - } - ]; - }; - }; + forAllSystems = nixpkgs.lib.genAttrs systems; + in { + packages = forAllSystems (system: rec { + packer = nixpkgs.legacyPackages.${system}.callPackage ./package.nix {inherit clj-nix;}; + default = packer; }); + + homeManagerModules.packer = import ./hm.module.nix self; + }; } diff --git a/hm.module.nix b/hm.module.nix new file mode 100644 index 0000000..c6a48d4 --- /dev/null +++ b/hm.module.nix @@ -0,0 +1,101 @@ +flake: { + config, + pkgs, + lib, + system, + ... +}: let + cfg = config.programs.packer; + templatePath = "${config.home.homeDirectory}/.config/packer/template.clj"; + + app = pkgs.runCommand "packer" {buildInputs = [pkgs.makeWrapper];} '' + makeWrapper ${flake.outputs.packages.${system}.packer}/bin/packer $out/bin/packer --set PACKER_TEMPLATE_FILE "${templatePath}" + ''; +in { + options.programs.packer = with lib; { + enable = mkEnableOption "Packer application"; + + templateFile = mkOption { + type = types.nullOr types.path; + description = "The path to template file. If provided, the 'template' option will be ignored."; + default = null; + example = "./packer.template.clj"; + }; + + template = mkOption { + type = types.str; + description = "The content of the template file written in Clojure language."; + default = '' + (use 'packer.model) + (require 'packer.util) + + ; Define some useful function + ; to provide a constant number of items. + (defn x [n] (fn [_] n)) + + ; Define a store - the single source + ; of truth of all items from which + ; we are going to choose what we want to pack. + ; The items are grouped by categories. + (def store { + :cloths [ + (->Item :t-shirt #(/ % 4)) + ] + + :electronics [ + (->Item :smartphone (x 1)) + (->Item :tablet (x 1)) + (->Item :laptop (x 1)) + ] + }) + + ; Define the display names for each + ; item, category and question. + (def i18n { + :nights? "How many nights?" + :tablet? "Do you need your tablet there?" + :laptop? "Do you need your laptop there?" + :electronics "Electronics" + :cloths "Cloths" + :t-shirt "T-shirt" + :smartphone "Smartphone" + :tablet "Tablet" + :laptop "Laptop" + }) + + ; Define some utility function + ; to generate preset basing on some question + (defn preset [question items seq] + (if (packer.util/ask-bool (get i18n question)) + (concat seq items) + seq)) + + ; Define profile - a function which takes + ; a number of provided nights and produces + ; the sequence of all items which should + ; be in the target list. If the sequence is empty + ; no item will be produced on the target list. + (defn profile [nights] + (->> [:smartphone :t-shirt] + (preset :tablet? [:tablet]) + (preset :laptop? [:laptop]))) + + ; Wrap it up together. It should be + ; the last expression in the file. + (->Template store i18n profile) + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [app]; + home.file.${templatePath} = + if (cfg.templateFile != null) + then { + source = cfg.templateFile; + } + else { + text = cfg.template; + }; + }; +} diff --git a/package.nix b/package.nix new file mode 100644 index 0000000..cfe911c --- /dev/null +++ b/package.nix @@ -0,0 +1,21 @@ +{ + pkgs, + clj-nix, + ... +}: +clj-nix.lib.mkCljApp { + inherit pkgs; + modules = [ + # Option list: + # https://jlesquembre.github.io/clj-nix/options/ + { + projectSrc = ./.; + name = "packer"; + main-ns = "packer.main"; + + nativeImage.enable = false; + + # customJdk.enable = true; + } + ]; +}