/ flake.nix
flake.nix
  1  {
  2    description = "Radicle web frontend";
  3    inputs = {
  4      nixpkgs.follows = "heartwood/nixpkgs";
  5      flake-utils.follows = "heartwood/flake-utils";
  6      heartwood = {
  7        url = "git+https://seed.radicle.xyz/z3gqcJUoA1n9HaHKufZs5FCSGazv5.git?ref=refs/namespaces/z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT/refs/tags/v1.0.0";
  8      };
  9    };
 10  
 11    outputs = {
 12      self,
 13      heartwood,
 14      nixpkgs,
 15      flake-utils,
 16      ...
 17    }@inputs:
 18      {
 19        nixosModules.radicle-explorer = { config, lib, pkgs, ... }: {
 20          options.services.radicle-explorer.enable = lib.mkEnableOption "Local radicle web interface";
 21          config = lib.mkIf config.services.radicle-explorer.enable {
 22            services.nginx = {
 23              enable = true;
 24              virtualHosts.localhost = {
 25                listen = [ { addr = "127.0.0.1"; port = 4433; ssl = false; } ];
 26                rejectSSL = true;
 27                locations = {
 28                  "/" = {
 29                    index = "index.html";
 30                    root = self.packages.${pkgs.system}.radicle-explorer;
 31                    extraConfig = ''
 32                      try_files $uri $uri/ /index.html;
 33                    '';
 34                  };
 35                };
 36              };
 37            };
 38          };
 39        };
 40      } //
 41  
 42      (flake-utils.lib.eachDefaultSystem (system: let
 43        pkgs = import nixpkgs {
 44          inherit system;
 45          overlays = [(import heartwood.inputs.rust-overlay)];
 46        };
 47        inherit (pkgs) lib;
 48  
 49        rustToolChain = pkgs.rust-bin.fromRustupToolchainFile radicle-httpd/rust-toolchain;
 50        craneLib = (heartwood.inputs.crane.mkLib pkgs).overrideToolchain rustToolChain;
 51        basicArgs = {
 52          pname = "radicle-httpd";
 53          src = ./radicle-httpd;
 54          strictDeps = true;
 55        };
 56      in {
 57  
 58        checks = {
 59          radicle-explorer =  self.packages.${system}.radicle-explorer.override { doCheck = true; };
 60          radicle-httpd = self.packages.${system}.radicle-httpd.overrideAttrs (_: { doCheck = true; });
 61        };
 62  
 63        packages = {
 64          default = self.packages.${system}.radicle-explorer;
 65  
 66          twemoji-assets = pkgs.fetchFromGitHub {
 67            owner = "twitter";
 68            repo = "twemoji";
 69            rev = "v14.0.2";
 70            hash = "sha256-YoOnZ5uVukzi/6bLi22Y8U5TpplPzB7ji42l+/ys5xI=";
 71          };
 72  
 73          radicle-explorer = pkgs.callPackage ({
 74            lib, buildNpmPackage, doCheck ? false
 75          }: buildNpmPackage rec {
 76            pname = "radicle-explorer";
 77            version = (builtins.fromJSON (builtins.readFile ./package.json)).version;
 78            src = ./.;
 79            npmDepsHash = "sha256-RC+QQXtvXC48uM0oOAFA0ni5AU/l9m8k1LgrxykSu5M=";
 80            postPatch = ''
 81              patchShebangs --build ./scripts
 82              mkdir -p "public/twemoji"
 83              cp -t public/twemoji -r -- ${self.packages.${system}.twemoji-assets}/assets/svg/*
 84              : >scripts/install-twemoji-assets
 85            '';
 86            dontConfigure = true;
 87  
 88            inherit doCheck;
 89            nativeCheckInputs = with pkgs; [
 90              which
 91              gitMinimal
 92            ];
 93            checkPhase = ''
 94              runHook preCheck
 95              bins=$(scripts/install-binaries -s)
 96              mkdir -p "$bins"
 97              cp -t "$bins" -- ${heartwood.packages.${system}.default}/bin/* ${self.checks.${system}.radicle-httpd}/bin/*
 98              scripts/check
 99              {
100                npm run test:unit
101              } | tee /dev/null
102              runHook postCheck
103            '';
104  
105            installPhase = ''
106              runHook preInstall
107              mkdir -p "$out"
108              cp -r -t "$out" build/*
109              runHook postInstall
110            '';
111          }) {};
112  
113          radicle-httpd = craneLib.buildPackage (basicArgs // {
114            inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle-httpd + "/Cargo.toml";}) pname version;
115            cargoArtifacts = craneLib.buildDepsOnly basicArgs;
116  
117            nativeBuildInputs = with pkgs;
118              [
119                git
120                # Add additional build inputs here
121                asciidoctor installShellFiles
122              ]
123              ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs; [
124                # Additional darwin specific inputs can be set here
125                libiconv
126                darwin.apple_sdk.frameworks.Security
127              ]);
128  
129            env =
130              {
131                RADICLE_VERSION = "nix-" + (self.shortRev or self.dirtyShortRev or "unknown");
132              }
133              // (
134                if self ? rev || self ? dirtyRev
135                then {
136                  GIT_HEAD = self.rev or self.dirtyRev;
137                }
138                else {}
139              );
140  
141            cargoExtraArgs = "-p radicle-httpd";
142            doCheck = false;
143            postInstall = ''
144              for page in radicle-httpd.1.adoc; do
145                asciidoctor -d manpage -b manpage $page
146                installManPage ''${page::-5}
147              done
148            '';
149          });
150        };
151      }));
152  }