From 311aca867e54d2fb9db8bdf1cd43d4000808089f Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Fri, 26 Apr 2024 23:03:15 +0100 Subject: [PATCH] chore: add nixos config --- flake.nix | 86 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/flake.nix b/flake.nix index b30f8b0..3dd456d 100644 --- a/flake.nix +++ b/flake.nix @@ -8,24 +8,74 @@ }; outputs = { self, nixpkgs, naersk, utils }: - utils.lib.eachDefaultSystem (system: - let - pkgs = import nixpkgs { inherit system; }; - naerskLib = pkgs.callPackage naersk { }; - in - { - defaultPackage = naerskLib.buildPackage ./.; - - devShell = with pkgs; mkShell { - buildInputs = [ cargo rustc rustfmt rustPackages.clippy ]; - RUST_SRC_PATH = rustPlatform.rustLibSrc; - }; + utils.lib.eachDefaultSystem + (system: + let + pkgs = import nixpkgs { inherit system; }; + naerskLib = pkgs.callPackage naersk { }; + in + { + packages = rec { + corn-api = naerskLib.buildPackage ./.; + default = corn-api; + }; + }) // { + nixosModules.default = { config, pkgs, ... }: + let + cfg = config.services.corn-api; + lib = pkgs.lib; + defaultPkg = self.packages.${pkgs.hostPlatform.system}.default; + in + { + options.services.corn-api = { + enable = lib.mkEnableOption "Web API for Corn configuration language"; + + package = lib.mkOption { + type = lib.types.package; + default = defaultPkg; + description = "The package to use"; + }; + + host = lib.mkOption { + type = lib.types.str; + description = "The hostname to listen on"; + default = "127.0.0.1"; + }; + + port = lib.mkOption { + type = lib.types.port; + description = "The port number to listen on"; + default = 5050; + }; + }; + + config = let pkg = cfg.package; in { + systemd.services.corn-api = lib.mkIf cfg.enable { + description = "Web API for Corn configuration language"; + documentation = [ "https://github.com/corn-config/corn-api" ]; - nixConfig = { - extra-substituters = [ "https://cache.garnix.io" ]; - extra-trusted-public-keys = - [ "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=" ]; + serviceConfig = { + ExecStart = "${pkg}/bin/corn-api"; + Restart = "on-failure"; + }; + + environment.HOST = cfg.host; + environment.PORT = toString cfg.port; + + wantedBy = [ "multi-user.target" ]; + }; + }; }; - } - ); + + devShell = with nixpkgs; mkShell { + buildInputs = [ cargo rustc rustfmt rustPackages.clippy ]; + RUST_SRC_PATH = rustPlatform.rustLibSrc; + }; + + nixConfig = { + extra-substituters = [ "https://cache.garnix.io" ]; + extra-trusted-public-keys = + [ "cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g=" ]; + }; + }; }