|
| 1 | +{ |
| 2 | + description = "Share file/text in your local network"; |
| 3 | + |
| 4 | + inputs = { |
| 5 | + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; |
| 6 | + }; |
| 7 | + |
| 8 | + outputs = { |
| 9 | + self, |
| 10 | + nixpkgs, |
| 11 | + ... |
| 12 | + }: let |
| 13 | + systems = [ |
| 14 | + "x86_64-linux" |
| 15 | + "aarch64-linux" |
| 16 | + "x86_64-darwin" |
| 17 | + "aarch64-darwin" |
| 18 | + ]; |
| 19 | + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); |
| 20 | + in { |
| 21 | + packages = forAllSystems ( |
| 22 | + system: let |
| 23 | + pkgs = import nixpkgs {inherit system;}; |
| 24 | + in { |
| 25 | + local-content-share = pkgs.buildGoModule { |
| 26 | + pname = "local-content-share"; |
| 27 | + version = ""; # nix needs version to be set, though setting it to the right one would mean it needs to be updated at each release |
| 28 | + src = ./.; |
| 29 | + vendorHash = null; |
| 30 | + |
| 31 | + meta = { |
| 32 | + mainProgram = "local-content-share"; |
| 33 | + description = "Self-hosted app for storing/sharing text/files in your local network with no setup on client devices"; |
| 34 | + homepage = "https://github.com/Tanq16/local-content-share"; |
| 35 | + }; |
| 36 | + }; |
| 37 | + default = self.packages.${system}.local-content-share; |
| 38 | + } |
| 39 | + ); |
| 40 | + |
| 41 | + nixosModules.local-content-share = { |
| 42 | + pkgs, |
| 43 | + lib, |
| 44 | + config, |
| 45 | + ... |
| 46 | + }: let |
| 47 | + cfg = config.services.local-content-share; |
| 48 | + in { |
| 49 | + options.services.local-content-share = { |
| 50 | + enable = lib.mkEnableOption "Local-Content-Share"; |
| 51 | + |
| 52 | + package = lib.mkPackageOption pkgs "local-content-share" {}; |
| 53 | + |
| 54 | + listenAddress = lib.mkOption { |
| 55 | + type = lib.types.str; |
| 56 | + default = ""; |
| 57 | + example = "127.0.0.1"; |
| 58 | + description = '' |
| 59 | + Address on which the service will be available. |
| 60 | +
|
| 61 | + The service will listen on all interfaces if set to an empty string. |
| 62 | + ''; |
| 63 | + }; |
| 64 | + |
| 65 | + port = lib.mkOption { |
| 66 | + type = lib.types.port; |
| 67 | + default = 8080; |
| 68 | + description = "Port on which the service will be available"; |
| 69 | + }; |
| 70 | + |
| 71 | + openFirewall = lib.mkOption { |
| 72 | + type = lib.types.bool; |
| 73 | + default = false; |
| 74 | + description = "Whether to automatically open the specified port in the firewall"; |
| 75 | + }; |
| 76 | + }; |
| 77 | + |
| 78 | + config = lib.mkIf cfg.enable { |
| 79 | + systemd.services.local-content-share = { |
| 80 | + after = ["network.target"]; |
| 81 | + wantedBy = ["multi-user.target"]; |
| 82 | + |
| 83 | + serviceConfig = { |
| 84 | + Type = "simple"; |
| 85 | + DynamicUser = true; |
| 86 | + User = "local-content-share"; |
| 87 | + StateDirectory = "local-content-share"; |
| 88 | + StateDirectoryMode = "0700"; |
| 89 | + WorkingDirectory = "/var/lib/local-content-share"; |
| 90 | + ExecStart = "${lib.getExe' cfg.package "local-content-share"} -listen=${cfg.listenAddress}:${toString cfg.port}"; |
| 91 | + Restart = "on-failure"; |
| 92 | + }; |
| 93 | + }; |
| 94 | + |
| 95 | + networking.firewall = lib.mkIf cfg.openFirewall { |
| 96 | + allowedTCPPorts = [cfg.port]; |
| 97 | + }; |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
| 101 | + nixosModules.default = self.nixosModules.local-content-share; |
| 102 | + }; |
| 103 | +} |
0 commit comments