From 9640543924d5034d65eb98e36c8332350d4ad26c Mon Sep 17 00:00:00 2001 From: Andrei Borzenkov Date: Sun, 6 Oct 2024 12:38:03 +0400 Subject: [PATCH] Create home-manager module for hbs2-peer and hbs2-git-dashboard --- README.md | 50 ++++++++++++++++++++++++++++++++++++ flake.nix | 1 + nix/hm-module.nix | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 nix/hm-module.nix diff --git a/README.md b/README.md index eb8dc45f..e7d97de5 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,8 @@ We’re using it for our non-public projects. ## How to install +### nix flakes + Assuming you know what the Nix and Nix flakes are ( See [nixos.org](https://nixos.org) if you don’t ) @@ -158,6 +160,54 @@ Alternative option: --substituters http://nix.hbs2.net:6000 \ --trusted-public-keys git.hbs2.net-1:HYIYU3xWetj0NasmHrxsWQTVzQUjawOE8ejZAW2xUS4= +### Home Manager module + +The following snippet of code tries to show how to bring the HBS2 flake +from the flake input and use its packages with Home Manager. + +Don’t forget to replace exampleName with your username! + +```nix +# flake.nix + +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + + home-manager = { + url = "github:nix-community/home-manager"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + hbs2.url = "git+https://git.hbs2.net/BTThPdHKF8XnEq4m6wzbKHKA6geLFK4ydYhBXAqBdHSP"; + }; + + outputs = {nixpkgs, home-manager, hbs2, ...}: { + homeConfigurations."exampleName" = + let system = "x86_64-linux"; # use your system here + in home-manager.lib.homeManagerConfiguration { + pkgs = nixpkgs.legacyPackages.${system}; + + modules = [ + hbs2.homeManagerModules.${system}.default + { + services.hbs2 = { + enable = true; + git-dashboard.enable = true; # optional + }; + } + # ... + ]; + }; + }; +} +``` + +Option `services.hbs2.enable` will add all hbs2 binaries into your environment and +create `hbs2-peer` user service to run it automatically at the background. + +Option `services.hbs2.git-dashboard.enable` will create `hbs2-git-dashboard` user service. + ## How to generate peer’s key? hbs2 keyring-new > new-peer-key.key diff --git a/flake.nix b/flake.nix index c765eb3e..1441bb2d 100644 --- a/flake.nix +++ b/flake.nix @@ -116,6 +116,7 @@ outputs = { self, nixpkgs, flake-utils, ... }@inputs: in { legacyPackages = pkgs; overlays.default = defaultOverlay; + homeManagerModules.default = import ./nix/hm-module.nix self; packages = packagesDynamic // diff --git a/nix/hm-module.nix b/nix/hm-module.nix new file mode 100644 index 00000000..8eaddbab --- /dev/null +++ b/nix/hm-module.nix @@ -0,0 +1,64 @@ +self: { + config, + lib, + pkgs, + ... }: let + inherit (pkgs.stdenv.hostPlatform) system; + + package = self.packages.${system}.default; + + cfg = config.services.hbs2; + hbs2 = cfg.package; + hbs2-peer = "${hbs2}/bin/hbs2-peer"; + hbs2-git-dashboard = "${hbs2}/bin/hbs2-git-dashboard"; +in { + options = { + services.hbs2 = { + enable = lib.mkEnableOption "hbs2-peer daemon"; + package = lib.mkOption { + type = lib.types.package; + description = "Package with all HBS2 basic binaries"; + default = package; + }; + git-dashboard.enable = lib.mkEnableOption "hbs2-git-dashboard daemon"; + }; + }; + config = lib.mkIf cfg.enable { + + home.packages = [ cfg.package ]; + + systemd.user.services.hbs2-peer = { + Unit = { + Description = "HBS2 peer daemon"; + After = [ "network.target" ]; + }; + Install = { + WantedBy = [ "default.target" ]; + }; + Service = { + ExecPreStart = "${hbs2-peer} init"; + ExecStart = "${hbs2-peer} run"; + Restart = "always"; + RuntimeMaxSec = "1d"; + }; + }; + + systemd.user.services.hbs2-git-dashboard = lib.mkIf cfg.git-dashboard.enable { + Unit = { + Description = "HBS2 git dashboard daemon"; + After = [ "hbs2-peer.service" "network.target" ]; + BindsTo = [ "hbs2-peer.service" ]; + }; + Install = { + WantedBy = [ "default.target" ]; + }; + Service = { + ExecPreStart = "sleep 5"; + ExecStart = "${hbs2-git-dashboard} web"; + Environment = [ "PATH=${hbs2}/bin:${pkgs.git}/bin:$PATH" ]; # detectRPC require hbs2-peer to be in scope + Restart = "always"; + RuntimeMaxSec = "1d"; + }; + }; + }; +}