From 9b10fde29dc13169417370e47b84d75b2bb79029 Mon Sep 17 00:00:00 2001 From: kossLAN Date: Sat, 1 Mar 2025 22:36:06 -0500 Subject: [PATCH] nix: added module & package for tinyupload --- flake.nix | 6 ++-- nix/module.nix | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ nix/package.nix | 27 +++----------- 3 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 nix/module.nix diff --git a/flake.nix b/flake.nix index b7ae6ed..095ab11 100644 --- a/flake.nix +++ b/flake.nix @@ -16,13 +16,13 @@ in { # Define a package for your project packages = forEachSystem (system: pkgs: rec { - tinyupload = pkgs.callPackage ./nix/package.nix { - inherit pkgs; - }; + tinyupload = pkgs.callPackage ./nix/package.nix {}; default = tinyupload; }); + nixosModules.default = import ./nix/module.nix self; + devShells = forEachSystem (system: pkgs: { default = pkgs.mkShell { DATABASE_URL = "sqlite:tinyupload.db"; diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..b629693 --- /dev/null +++ b/nix/module.nix @@ -0,0 +1,95 @@ +# Untested, but should be the majority of what is needed. +self: { + lib, + config, + pkgs, + ... +}: let + inherit (lib.options) mkEnableOption mkOption; + inherit (lib.types) str path port int; + inherit (lib.modules) mkIf; + + cfg = config.services.tinyupload; +in { + options.services.tinyupload = { + enable = mkEnableOption "Enable tinyupload."; + + address = mkOption { + type = str; + default = "127.0.0.1"; + description = "Address on which tinyupload will listen on"; + }; + + port = mkOption { + type = port; + default = 8080; + description = "Port on which the tinyupload service will listen"; + }; + + uploadLimit = mkOption { + type = int; + default = 100; + description = "Upload limit for tinyupload in MB"; + }; + + title = mkOption { + type = str; + default = "tinyupload"; + description = "Title for the frontend of tinyupload"; + }; + + dataDir = mkOption { + type = path; + default = "/var/lib/tinyupload"; + description = "Directory to store uploaded files and database"; + }; + + user = mkOption { + type = str; + default = "tinyupload"; + description = "User account under which the service will run"; + }; + + group = mkOption { + type = str; + default = "tinyupload"; + description = "Group under which the service will run"; + }; + }; + + config = mkIf cfg.enable { + users.users.${cfg.user} = { + isSystemUser = true; + group = cfg.group; + home = cfg.dataDir; + createHome = true; + description = "tinyupload service user"; + }; + + users.groups.${cfg.group} = {}; + + systemd.services.tinyupload = { + description = "tinyupload service"; + wantedBy = ["multi-user.target"]; + after = ["network.target"]; + + serviceConfig = { + RestartSec = 5; + User = cfg.user; + Group = cfg.group; + WorkingDirectory = cfg.dataDir; + Restart = "on-failure"; + ExecStart = "${(pkgs.writeShellApplication { + name = "start-tinyupload"; + text = '' + ${self.packages.${pkgs.system}.tinyupload}/bin/tinyupload \ + -u ${toString cfg.uploadLimit} \ + -a "${cfg.address}:${toString cfg.port}" \ + -t "${cfg.title}" \ + serve + ''; + })}/bin/start-tinyupload"; + }; + }; + }; +} diff --git a/nix/package.nix b/nix/package.nix index 7215986..4247538 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -1,26 +1,7 @@ -{ - stdenv, - pkgs, - ... -}: -stdenv.mkDerivation { +{rustPlatform, ...}: +rustPlatform.buildRustPackage { pname = "tinyupload"; - version = "1.0.0"; - + version = "0.2.0"; src = ../.; - - buildInputs = [pkgs.cargo]; - - # Specify the build commands - buildPhase = '' - cargo build --release - ''; - - # Optionally, specify installPhase if needed - installPhase = '' - # If you have specific install steps, add them here - # For example, copying files to $out - mkdir -p $out/bin - cp target/release/tinyupload $out/bin - ''; + cargoHash = "sha256-nfrci/E/ZSVfPLEUIKBRgCGkxH2p7WgnwDBHwOsUnCw="; }