From feb74fdf2d28bc4494298c0a97d7f02368290cf6 Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Mon, 19 Dec 2022 18:26:14 +1000 Subject: [PATCH] flake that i'd already made for this --- flake.nix | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 flake.nix diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..00146f7 --- /dev/null +++ b/flake.nix @@ -0,0 +1,115 @@ +{ + description = "Reproducible Environment for Grasp server"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + grasp.url = "github:karlicoss/grasp"; + grasp.flake = false; + }; + + outputs = { self, nixpkgs, flake-utils, grasp }: + flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + packages.grasp = pkgs.stdenv.mkDerivation rec { + pname = "grasp"; + version = "v0.6.6"; + src = grasp; + + installPhase = '' + mkdir -p $out/bin + cp -r server/* $out/bin/ + ''; + }; + devShell = pkgs.mkShell { + buildInputs = [ pkgs.python3 ]; + }; + }) // { + nixosModule = { options, lib, config, pkgs, ... }: + let + graspConfig = config.services."grasp"; + + options = { + enable = lib.mkEnableOption "Grasp service"; + path = lib.mkOption rec { + type = lib.types.path; + default = "/var/lib/grasp/"; + example = "/var/lib/grasp"; + description = "Where to run Grasp from"; + }; + #port = lib.mkOption rec { + # type = lib.types.int; + # default = 12212; + # example = 12212; + # description = "Port to run the server on"; + #}; + user = lib.mkOption rec { + type = lib.types.str; + default = ""; + example = "nixos"; + description = "User to run the service as"; + }; + #template = mkOption rec { + # type = types.str; + # default = "12212"; + # example = "12212"; + # description = "Template to use"; + #}; + }; + in + { + options.services.grasp-server = options; + config = lib.mkIf graspConfig.enable { + systemd.services.grasp-server = { + description = '' + A grasp server module + ''; + wantedBy = [ "multi-user.target" ]; + after = [ "network.target" ]; + serviceConfig = { + #DynamicUser = true; # Probably should default to this if no user supplied + User = "${graspConfig.user}"; + RuntimeDirectory = "${graspConfig.path}"; + RuntimeDirectoryMode = "0755"; + StateDirectory = "${graspConfig.path}"; + StateDirectoryMode = "0700"; + }; + path = with pkgs; [ nix python3 ] ++ [ ]; + script = '' + ${self.packages.${pkgs.system}.grasp}/bin/grasp_server.py --path ${graspConfig.path} + ''; + }; + # Allow nginx through the firewall + networking.firewall.allowedTCPPorts = [ 12212 ]; + }; + }; + # Test container + nixosConfigurations."test" = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + self.nixosModule + ({ pkgs, ... }: { + # Only allow this to boot as a container + boot.isContainer = true; + networking.hostName = "grasp"; + services."grasp".enable = true; + services."grasp".user = "anish"; + services."grasp".path = "/home/anish/grasp/capture.org"; + # I think I actually created the above folder as well in the container + # I figured dynamic user with their own text file seems less useful + # I didn't know how to default to a + users.users.anish = { + isNormalUser = true; + password = "password"; + }; + }) + ]; + }; + }; +} + +