summary refs log tree commit diff
path: root/flake.nix
blob: b437ff139883bcf97c7e462028b490c5316a3e64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
  inputs = {
    utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:NixOS/nixpkgs/master";
    nix-filter.url = "github:numtide/nix-filter";
  };

  outputs = {
    self,
    nixpkgs,
    utils,
    nix-filter,
  }:
    utils.lib.eachDefaultSystem
    (system: let
      pkgs = import nixpkgs {
        inherit system;
      };

      filter = nix-filter.lib;

      nodejs = pkgs.nodejs_20;

      src = filter {
        root = ./.;
        include = [
          ./package.json
          ./package-lock.json
        ];
      };

      packageLock = builtins.fromJSON(builtins.readFile (src + "/package-lock.json"));

      deps = builtins.attrValues (removeAttrs packageLock.packages [ "" ]);
      depTarballs = map (p: pkgs.fetchurl { url = p.resolved; hash = p.integrity; }) deps;
      nodeTarball = pkgs.fetchurl {
        url = "https://nodejs.org/download/release/v20.11.1/node-v20.11.1-headers.tar.gz";
        hash = "sha256-CqQskbRB6UX/Q706g3dZxYtDbeV9zQM9AuXLzS+6H4c=";
      };
      tarballsFile = pkgs.writeTextFile {
        name = "tarballs";
        text = (builtins.concatStringsSep "\n" depTarballs) + "\n";
      };

      tempestdev_modules = pkgs.stdenv.mkDerivation {
        inherit src;

        name = "tempestdevnode_modules";

        nativeBuildInputs = [ 
          nodejs
          pkgs.nodePackages.node-gyp
          pkgs.vips 
          pkgs.pkg-config
          pkgs.python3
        ];

        buildPhase = ''
          export HOME=$PWD/.home
          export npm_config_cache=$PWD/.npm
          NODE_VERSION=$(${nodejs}/bin/node -v)

          echo "Caching Node headers"
          ${pkgs.nodePackages.node-gyp}/bin/node-gyp install $NODE_VERSION --tarball=${nodeTarball}

          echo "Caching NPM dependencies"
          while read package
          do
            npm cache add "$package"
          done <${tarballsFile}

          ${nodejs}/bin/npm --offline ci --no-registry
        '';

        installPhase = ''
          mkdir $out
          sed --in-place 's|/usr/bin/env node|${nodejs}/bin/node|' ./node_modules/next/dist/bin/next
          mv node_modules $out/node_modules
        '';
      };
    in {
      packages = {

        default = pkgs.stdenv.mkDerivation {
          name = "tempest.dev";

          src = filter {
            root = ./.;
            exclude = [
              ./.next
              ./node_modules
            ];
          };

          nativeBuildInputs = [ nodejs ];

          configurePhase = ''
            ln -sf ${tempestdev_modules}/node_modules node_modules
            export HOME=$TMP
          '';

          buildPhase = ''
            npm run build
          '';

          installPhase = ''
            mv out $out
          '';
        };
      };
    }) // {
      nixosModule = {config, lib, pkgs, ...}:
        with lib;
        let cfg = config.ashe.services."tempest.dev";
            pkg = self.packages.${pkgs.system}.default;


        in {
          options.ashe.services."tempest.dev" = {
            enable = mkEnableOption "Enables the tempest.dev HTTP service";

            domain = mkOption rec {
              type = types.str;
              default = "tempest.dev";
              example = default;
              description = "The domain name for tempest.dev";
            };
          };

          config = mkIf cfg.enable {
            services.nginx.virtualHosts.${cfg.domain} = {
              locations."/" = {
                root = "${pkg}";
              };
              locations."/.well-known/" = {
                extraConfig = ''
                  alias ${pkg}/.well-known/;
                  add_header Content-Type text/plain;
                '';
              };
              forceSSL = true;
              enableACME = true;
            };
          };
        };
    };
}