From d41b55e44c41824d11f85bfeb0637aa061dbfbc0 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 28 Mar 2024 10:02:01 -0600 Subject: [PATCH] switch to flakes, add a module --- .envrc | 2 +- .gitignore | 1 + flake.lock | 26 +++++++++++++++++++++ flake.nix | 54 +++++++++++++++++++++++++++++++++++++++++++ module.nix | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 12 ---------- 6 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 module.nix delete mode 100644 shell.nix diff --git a/.envrc b/.envrc index 4a4726a..3550a30 100644 --- a/.envrc +++ b/.envrc @@ -1 +1 @@ -use_nix +use flake diff --git a/.gitignore b/.gitignore index 1659833..1972a79 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ db/* mcchunkie deploy.sh *.core +.direnv diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c067efc --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1711523803, + "narHash": "sha256-UKcYiHWHQynzj6CN/vTcix4yd1eCu1uFdsuarupdCQQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2726f127c15a4cc9810843b96cad73c7eb39e443", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..d8ae0d3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,54 @@ +{ + description = "mcchunkie: a chat bot"; + + inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; + + outputs = + { self + , nixpkgs + , + }: + let + supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); + in + { + overlay = _: prev: { inherit (self.packages.${prev.system}) mcchunkie; }; + nixosModule = import ./module.nix; + packages = forAllSystems (system: + let + pkgs = nixpkgsFor.${system}; + in + { + mcchunkie = pkgs.buildGoModule rec { + pname = "mcchunkie"; + version = "v1.0.15"; + src = ./.; + + vendorHash = "sha256-LMWaHqmvxG17Z0zh0vmTAYDsc9UkztSCM1+jl2iXSds="; + + # makes outbound http requests + doCheck = false; + + ldflags = [ "-X suah.dev/mcchunkie/plugins.version=${version}" ]; + }; + }); + + defaultPackage = forAllSystems (system: self.packages.${system}.mcchunkie); + devShells = forAllSystems (system: + let + pkgs = nixpkgsFor.${system}; + in + { + default = pkgs.mkShell { + shellHook = '' + PS1='\u@\h:\@; ' + nix run github:qbit/xin#flake-warn + echo "Go `${pkgs.go}/bin/go version`" + ''; + nativeBuildInputs = with pkgs; [ git go gopls go-tools ]; + }; + }); + }; +} diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..0919e38 --- /dev/null +++ b/module.nix @@ -0,0 +1,67 @@ +{ lib, config, pkgs, ... }: +let cfg = config.services.mcchunkie; +in { + options = with lib; { + services.mcchunkie = { + enable = lib.mkEnableOption "Enable mcchunkie"; + + user = mkOption { + type = with types; oneOf [ str int ]; + default = "mcchunkie"; + description = '' + The user the service will use. + ''; + }; + + group = mkOption { + type = with types; oneOf [ str int ]; + default = "mcchunkie"; + description = '' + The group the service will use. + ''; + }; + + dataDir = mkOption { + type = types.path; + default = "/var/lib/mcchunkie"; + description = "Path mcchunkie will use to store data"; + }; + + package = mkOption { + type = types.package; + default = pkgs.mcchunkie; + defaultText = literalExpression "pkgs.mcchunkie"; + description = "The package to use for mcchunkie"; + }; + }; + }; + + config = lib.mkIf (cfg.enable) { + users.groups.${cfg.group} = { }; + users.users.${cfg.user} = { + description = "mcchunkie service user"; + isSystemUser = true; + home = "${cfg.dataDir}"; + createHome = true; + group = "${cfg.group}"; + }; + + systemd.services.mcchunkie = { + enable = true; + description = "mcchunkie server"; + wantedBy = [ "network-online.target" ]; + after = [ "network-online.target" ]; + + environment = { HOME = "${cfg.dataDir}"; }; + + serviceConfig = { + User = cfg.user; + Group = cfg.group; + + ExecStart = '' + ${cfg.package}/bin/mcchunkie + ''; + }; + }; + }; +} diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 4a56637..0000000 --- a/shell.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ pkgs ? import { } }: -pkgs.mkShell { - shellHook = '' - export NO_COLOR=true - export PS1="\u@\h:\w; " - ''; - - nativeBuildInputs = with pkgs.buildPackages; [ - go - go-tools - ]; -}