From 5a54a3b4d78b6404d011f9dd65827f5f538e54ee Mon Sep 17 00:00:00 2001 From: Luna Simons Date: Wed, 24 Jan 2024 02:16:30 +0100 Subject: [PATCH] feat: nixification (#28) --- default.nix | 53 +++++++++++++++++++++++++++++++----------------- flake.lock | 50 +++++++++++++++++++++++++++++++++++++++------ flake.nix | 57 +++++++++++++++++++++++++++++----------------------- module.nix | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +-- 5 files changed, 169 insertions(+), 51 deletions(-) create mode 100644 module.nix diff --git a/default.nix b/default.nix index 4c3c895..e592c2a 100644 --- a/default.nix +++ b/default.nix @@ -1,23 +1,40 @@ -{pkgs ? import {}}: -with pkgs; - mkYarnPackage rec { - pname = "untis-ics-sync"; - version = "0.5.10"; +{ + mkYarnPackage, + fetchYarnDeps, + makeWrapper, + lib, + nodejs, +}: +mkYarnPackage rec { + pname = "untis-ics-sync"; + version = "0.6.0"; - src = ./.; + src = ./.; - offlineCache = fetchYarnDeps { - yarnLock = src + "/yarn.lock"; - hash = "sha256-NHghkf5Nziyz3M7E4941sV5JFqY7RYMTlZqYsQPZLpU="; - }; + offlineCache = fetchYarnDeps { + yarnLock = src + "/yarn.lock"; + hash = "sha256-NHghkf5Nziyz3M7E4941sV5JFqY7RYMTlZqYsQPZLpU="; + }; - packageJSON = ./package.json; - yarnLock = ./yarn.lock; + nativeBuildInputs = [makeWrapper]; - buildPhase = '' - export HOME=$(mktemp -d) - yarn --offline build - ''; + packageJSON = ./package.json; + yarnLock = ./yarn.lock; - distPhase = "true"; - } + buildPhase = '' + yarn --offline run build + ''; + + postInstall = '' + makeWrapper ${lib.getExe nodejs} "$out/bin/untis-ics-sync" \ + --add-flags "$out/libexec/untis-ics-sync/deps/untis-ics-sync/dist/main.js" + ''; + + meta = with lib; { + description = "Serves a calendar API (ICS) for events provided from Untis"; + homepage = "https://github.com/bddvlpr/untis-ics-sync"; + license = licenses.bsd3; + maintainers = with maintainers; [bddvlpr]; + mainProgram = "untis-ics-sync"; + }; +} diff --git a/flake.lock b/flake.lock index 3abb6da..98f8c86 100644 --- a/flake.lock +++ b/flake.lock @@ -1,22 +1,60 @@ { "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1704982712, + "narHash": "sha256-2Ptt+9h8dczgle2Oo6z5ni5rt/uLMG47UFTR1ry/wgg=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "07f6395285469419cf9d078f59b5b49993198c00", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1693654884, - "narHash": "sha256-EqKKEl+IOS8TSjkt+xn1qGpsjnx5/ag33YNQ1+c7OuM=", - "owner": "NixOS", + "lastModified": 1705883077, + "narHash": "sha256-ByzHHX3KxpU1+V0erFy8jpujTufimh6KaS/Iv3AciHk=", + "owner": "nixos", "repo": "nixpkgs", - "rev": "e7f35e03abd06a2faef6684d0de813370e13bda8", + "rev": "5f5210aa20e343b7e35f40c033000db0ef80d7b9", "type": "github" }, "original": { - "id": "nixpkgs", + "owner": "nixos", "ref": "nixpkgs-unstable", - "type": "indirect" + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1703961334, + "narHash": "sha256-M1mV/Cq+pgjk0rt6VxoyyD+O8cOUiai8t9Q6Yyq4noY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b0d36bd0a420ecee3bc916c91886caca87c894e9", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" } }, "root": { "inputs": { + "flake-parts": "flake-parts", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index 87411c8..22d931a 100644 --- a/flake.nix +++ b/flake.nix @@ -1,33 +1,40 @@ { inputs = { - nixpkgs.url = "nixpkgs/nixpkgs-unstable"; + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; }; - outputs = {nixpkgs, ...}: let - forAllSystems = nixpkgs.lib.genAttrs [ - "aarch64-linux" - "aarch64-darwin" - "x86_64-darwin" - "x86_64-linux" - ]; - in { - formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.alejandra); + outputs = inputs @ { + self, + flake-parts, + ... + }: + flake-parts.lib.mkFlake {inherit inputs;} { + systems = [ + "aarch64-linux" + "aarch64-darwin" + "x86_64-darwin" + "x86_64-linux" + ]; - packages = forAllSystems ( - system: let - pkgs = nixpkgs.legacyPackages.${system}; - in { - default = import ./default.nix {inherit pkgs;}; - } - ); + perSystem = {pkgs, ...}: { + formatter = pkgs.alejandra; - devShell = forAllSystems ( - system: let - pkgs = nixpkgs.legacyPackages.${system}; - in - pkgs.mkShell { + packages = rec { + default = pkgs.callPackage ./default.nix {}; + untis-ics-sync = default; + }; + + devShells.default = pkgs.mkShell { buildInputs = with pkgs; [nodejs yarn nest-cli]; - } - ); - }; + }; + }; + + flake = { + nixosModules = rec { + default = import ./module.nix self; + untis-ics-sync = default; + }; + }; + }; } diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..bdff6d0 --- /dev/null +++ b/module.nix @@ -0,0 +1,57 @@ +self: { + config, + lib, + pkgs, + ... +}: let + cfg = config.services.untis-ics-sync; +in { + options.services.untis-ics-sync = let + inherit (lib) mkEnableOption mkPackageOption mkOption types; + inherit (pkgs.stdenv.hostPlatform) system; + in { + enable = mkEnableOption "Untis ICS Sync"; + package = mkPackageOption self.packages.${system} "default" {}; + + envFile = mkOption { + type = types.path; + description = "The environment file with credentials."; + }; + + user = mkOption { + type = types.str; + default = "untis-ics-sync"; + description = "The user to run untis-ics-sync under."; + }; + group = mkOption { + type = types.str; + default = "untis-ics-sync"; + description = "The group to run untis-ics-sync under."; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.untis-ics-sync = { + description = "untis-ics-sync"; + wantedBy = ["multi-user.target"]; + wants = ["network-online.target"]; + after = ["network-online.target"]; + + serviceConfig = { + Restart = "on-failure"; + ExecStart = "${lib.getExe cfg.package}"; + EnvironmentFile = cfg.envFile; + User = cfg.user; + Group = cfg.group; + }; + }; + + users = { + users.untis-ics-sync = lib.mkIf (cfg.user == "untis-ics-sync") { + isSystemUser = true; + group = cfg.group; + }; + groups.untis-ics-sync = lib.mkIf (cfg.group == "untis-ics-sync") {}; + }; + }; +} diff --git a/package.json b/package.json index 1fe6509..cdf1019 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,8 @@ { "name": "untis-ics-sync", - "version": "0.5.10", + "version": "0.6.0", "description": "Serves a calendar API (ICS) for events provided from Untis.", "author": "Luna Simons (https://bddvlpr.com)", - "bin": "dist/main.js", "license": "BSD-3-Clause", "private": true, "scripts": {