diff --git a/grasp/flake.lock b/grasp/flake.lock new file mode 100644 index 0000000..d0e5a6f --- /dev/null +++ b/grasp/flake.lock @@ -0,0 +1,59 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1652372896, + "narHash": "sha256-lURGussfF3mGrFPQT3zgW7+RC0pBhbHzco0C7I+ilow=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "0d347c56f6f41de822a4f4c7ff5072f3382db121", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "grasp": { + "flake": false, + "locked": { + "lastModified": 1586706168, + "narHash": "sha256-xLkxE24xtTghQpAfAqAsOvsGpz0coACXjNT8bHZhcxM=", + "owner": "karlicoss", + "repo": "grasp", + "rev": "4bc8f011227d1baa18b4e98543561534065490d8", + "type": "github" + }, + "original": { + "owner": "karlicoss", + "repo": "grasp", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1652513317, + "narHash": "sha256-2cYDMHAVo/q1pUZ2m5SH0UdT1ydZ4RKWcmof9VvltzI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "4f1c9b02c48f888b09317e32c29bffeb9ad2c925", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "grasp": "grasp", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/grasp/flake.nix b/grasp/flake.nix new file mode 100644 index 0000000..256acf4 --- /dev/null +++ b/grasp/flake.nix @@ -0,0 +1,139 @@ +{ + 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}; + grasp-pkg = pkgs.stdenv.mkDerivation rec { + pname = "Grasp"; + version = "v0.6.6"; + src = grasp; + meta = with pkgs.lib; { + description = ""; + homepage = "https://github.com/karlicoss/grasp"; + # license = with licenses; [ mit ]; + maintainers = with maintainers; [ chickensoupwithrice ]; + }; + + installPhase = '' + mkdir -p $out/bin + cp -r server/* $out/bin/ + ''; + }; + python-with-my-packages = pkgs.python39.withPackages (p: with p; [ + pytz # even though it's not needed by the core, it's so common anyway... + appdirs # very common, and makes it portable + more-itertools # it's just too useful and very common anyway + decorator # less pain in writing correct decorators. very mature and stable, so worth keeping in core + click + setuptools-scm + pytest + geopy + mypy + ]); + in + { + devShell = pkgs.mkShell { + buildInputs = [ + python-with-my-packages + grasp-pkg + ]; + shellHook = '' + PYTHONPATH=${python-with-my-packages}/${python-with-my-packages.sitePackages} + # maybe set more env-vars + ''; + }; + + defaultPackage = grasp-pkg; + }) // { + 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.str; + 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" = 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}"; + ExecStart = '' + ${self.defaultPackage.${pkgs.system}}/bin/grasp_server.py --path ${graspConfig.path} --port ${graspConfig.port} + ''; + RuntimeDirectory = "${graspConfig.path}"; + RuntimeDirectoryMode = "0755"; + StateDirectory = "${graspConfig.path}"; + StateDirectoryMode = "0700"; + }; + path = with pkgs; [ nix python3 ] ++ [ self.defaultPackage.${pkgs.system} ]; + }; + # 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"; + }; + }) + ]; + }; + }; +}