1{
2 inputs = {
3 nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
4 };
5
6 outputs = {
7 self,
8 nixpkgs,
9 }: let
10 supportedSystems = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"];
11 forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
12 nixpkgsFor = forAllSystems (system:
13 import nixpkgs {
14 inherit system;
15 overlays = [self.overlays.default];
16 });
17 in {
18 overlays.default = final: prev: let
19 pname = "plonk";
20 version = "0.1.0";
21 in {
22 plonk = with final;
23 buildNpmPackage {
24 inherit pname version;
25 src = ./.;
26 nativeBuildInputs = [makeBinaryWrapper];
27 packageJson = ./package.json;
28 buildPhase = "npm run build";
29 installPhase = ''
30 runHook preInstall
31
32 mkdir -p $out/bin
33 cp -r ./* $out/
34
35 makeBinaryWrapper ${nodejs}/bin/node $out/bin/$pname \
36 --prefix PATH : ${lib.makeBinPath [nodejs]} \
37 --add-flags "$out/dist/index.js"
38 '';
39
40 npmDepsHash = "sha256-qGCbaFAHd/s9hOTWMjHCam6Kf6pU6IWPybfwYh0sOwc=";
41 };
42 };
43
44 packages = forAllSystems (system: {
45 inherit (nixpkgsFor."${system}") plonk;
46 });
47
48 defaultPackage = forAllSystems (system: nixpkgsFor."${system}".plonk);
49
50 devShell = forAllSystems (system: let
51 pkgs = nixpkgsFor."${system}";
52 in
53 pkgs.mkShell {
54 nativeBuildInputs = [
55 pkgs.nodejs
56 pkgs.biome
57 pkgs.typescript
58 pkgs.nodePackages.typescript-language-server
59 ];
60 });
61
62 formatter = forAllSystems (system: nixpkgsFor."${system}".alejandra);
63
64 nixosModules.default = {
65 config,
66 pkgs,
67 lib,
68 ...
69 }:
70 with lib; {
71 options = {
72 services.plonk = {
73 enable = mkOption {
74 type = types.bool;
75 default = false;
76 description = "Enable plonk";
77 };
78 port = mkOption {
79 type = types.int;
80 default = 3000;
81 description = "Port to run plonk on";
82 };
83 cookie_secret = mkOption {
84 type = types.str;
85 default = "00000000000000000000000000000000";
86 description = "Cookie secret";
87 };
88 };
89 };
90
91 config = mkIf config.services.plonk.enable {
92 nixpkgs.overlays = [self.overlays.default];
93 systemd.services.plonk = {
94 description = "plonk service";
95 wantedBy = ["multi-user.target"];
96
97 serviceConfig = {
98 ListenStream = "0.0.0.0:${toString config.services.plonk.port}";
99 ExecStart = "${pkgs.plonk}/bin/plonk";
100 Restart = "always";
101 };
102
103 environment = {
104 PLONK_PORT = "${toString config.services.plonk.port}";
105 PLONK_NODE_ENV = "production";
106 PLONK_HOST = "localhost";
107 PLONK_PUBLIC_URL = "https://plonk.li";
108 PLONK_DB_PATH = "plonk.db";
109 PLONK_COOKIE_SECRET = config.services.plonk.cookie_secret;
110 };
111 };
112 };
113 };
114 };
115}