From c6b0fdd001ecf3673c810ecb8a434ee5add10b57 Mon Sep 17 00:00:00 2001
From: Eduardo Trujillo <ed@chromabits.com>
Date: Sun, 8 Jan 2023 16:11:18 -0800
Subject: [PATCH] feat: Add Nix Flake

---
 Cargo.toml       |  3 ++
 flake.lock       | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
 flake.nix        | 40 +++++++++++++++++++++++++
 home-manager.nix | 48 ++++++++++++++++++++++++++++++
 4 files changed, 167 insertions(+)
 create mode 100644 flake.lock
 create mode 100644 flake.nix
 create mode 100644 home-manager.nix

diff --git a/Cargo.toml b/Cargo.toml
index 6fa011a..fdd9fc1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,9 @@ description = "A NetworkManager reactor. If-this-then-that for NetworkManager."
 repository = "https://gitlab.chromabits.com/etcinit/nm-reactor"
 exclude = ["third_party"]
 
+[workspace]
+members = ["dbus_codegen"]
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..1d5d507
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,76 @@
+{
+  "nodes": {
+    "flake-utils": {
+      "locked": {
+        "lastModified": 1667395993,
+        "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
+        "type": "github"
+      },
+      "original": {
+        "owner": "numtide",
+        "repo": "flake-utils",
+        "type": "github"
+      }
+    },
+    "naersk": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      },
+      "locked": {
+        "lastModified": 1671096816,
+        "narHash": "sha256-ezQCsNgmpUHdZANDCILm3RvtO1xH8uujk/+EqNvzIOg=",
+        "owner": "nix-community",
+        "repo": "naersk",
+        "rev": "d998160d6a076cfe8f9741e56aeec7e267e3e114",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-community",
+        "repo": "naersk",
+        "type": "github"
+      }
+    },
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1673180965,
+        "narHash": "sha256-gMhL6w9RVluvPs+irJ9n0Q1BphZm+Ek4XGn5Ow7YQ3k=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "0c9aadc8eff6daaa5149d2df9e6c49baaf44161c",
+        "type": "github"
+      },
+      "original": {
+        "id": "nixpkgs",
+        "type": "indirect"
+      }
+    },
+    "nixpkgs_2": {
+      "locked": {
+        "lastModified": 1673180965,
+        "narHash": "sha256-gMhL6w9RVluvPs+irJ9n0Q1BphZm+Ek4XGn5Ow7YQ3k=",
+        "owner": "NixOS",
+        "repo": "nixpkgs",
+        "rev": "0c9aadc8eff6daaa5149d2df9e6c49baaf44161c",
+        "type": "github"
+      },
+      "original": {
+        "owner": "NixOS",
+        "ref": "nixpkgs-unstable",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "flake-utils": "flake-utils",
+        "naersk": "naersk",
+        "nixpkgs": "nixpkgs_2"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..21bdf66
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,40 @@
+{
+  inputs = {
+    flake-utils.url = "github:numtide/flake-utils";
+    naersk.url = "github:nix-community/naersk";
+    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
+  };
+
+  outputs = {
+    self,
+    flake-utils,
+    naersk,
+    nixpkgs,
+  }:
+    flake-utils.lib.eachDefaultSystem (
+      system: let
+        pkgs = (import nixpkgs) {
+          inherit system;
+        };
+
+        naersk' = pkgs.callPackage naersk {};
+      in rec {
+        defaultPackage = naersk'.buildPackage {
+          src = ./.;
+          buildInputs = with pkgs; [dbus];
+          nativeBuildInputs = with pkgs; [pkg-config];
+        };
+
+        devShell = pkgs.mkShell {
+          buildInputs = with pkgs; [dbus];
+          nativeBuildInputs = with pkgs; [rustc cargo pkg-config];
+        };
+
+        formatter = nixpkgs.legacyPackages.${system}.alejandra;
+      }
+    )
+    // {
+      nixosModules.home-manager.nm-reactor = import ./home-manager.nix self;
+    };
+}
+
diff --git a/home-manager.nix b/home-manager.nix
new file mode 100644
index 0000000..853ccf6
--- /dev/null
+++ b/home-manager.nix
@@ -0,0 +1,48 @@
+outputs: {
+  config,
+  lib,
+  pkgs,
+  ...
+}:
+with lib; let
+  cfg = config.services.nm-reactor;
+in {
+  options.services.nm-reactor = {
+    enable = mkEnableOption "Enables the nm-reactor daemon";
+
+    package = mkOption {
+      type = types.package;
+      default = outputs.defaultPackage.${pkgs.system};
+      description = mdDoc "The `nm-reactor` package to use.";
+    };
+
+    configLines = mkOption {
+      type = types.lines;
+      description = mdDoc "The contents of the configuration file";
+    };
+  };
+
+  config = mkIf cfg.enable {
+    xdg.configFile."nm-reactor/config.yaml" = {
+      text = cfg.configLines;
+    };
+
+    home.packages = [cfg.package];
+
+    systemd.user.services.nm-reactor = {
+      Unit = {
+        Description = ''
+          A NetworkManager reactor. If-this-then-that for NetworkManager.
+        '';
+        Requires = ["NetworkManager.service"];
+      };
+
+      Service = {
+        ExecStart = "${cfg.package}/bin/nm-reactor";
+        Environment = "RUST_LOG=info";
+      };
+
+      Install.WantedBy = ["default.target"];
+    };
+  };
+}
-- 
GitLab