aboutsummaryrefslogtreecommitdiffhomepage
path: root/pkgs/mo/default.nix
blob: 9f62deb2ce1d57ff3a8e22009a04a00438e1a0b5 (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
{
  lib,
  stdenvNoCC,
  buildGoModule,
  fetchFromGitHub,
  fetchPnpmDeps,
  jq,
  nodejs_22,
  pnpm_10,
  pnpmConfigHook,
}:

let
  version = "1.6.1";

  src = fetchFromGitHub {
    owner = "k1LoW";
    repo = "mo";
    rev = "v${version}";
    hash = "sha256-/PiMYllj0l3XwIkqT/sc7U/vGXdNmTD8RowZWe9ZDR8=";
  };

  frontend = stdenvNoCC.mkDerivation (finalAttrs: {
    pname = "mo-frontend";
    inherit version src;
    sourceRoot = "${src.name}/internal/frontend";

    nativeBuildInputs = [
      jq
      nodejs_22
      pnpm_10
      pnpmConfigHook
    ];

    # Remove .pnpm.executionEnv to prevent pnpm from downloading Node.js binary.
    # Drop pnpm's executionEnv.nodeVersion pin. With it set, pnpm insists on
    # managing its own Node.js and downloads it from nodejs.org at build time
    # (which fails offline) -- it does this even when the pinned version equals
    # the Node.js already on PATH, so matching the version does not help. Remove
    # the pin instead and let pnpm use the nixpkgs Node.js from nativeBuildInputs.
    postPatch = ''
      jq 'del(.pnpm.executionEnv)' package.json > package.json.tmp
      mv package.json.tmp package.json

      chmod -R u+w ../static
    '';

    pnpmDeps = fetchPnpmDeps {
      inherit (finalAttrs)
        pname
        version
        src
        sourceRoot
        ;
      pnpm = pnpm_10;
      fetcherVersion = 3;
      hash = "sha256-473ftja7cEzU/FrMN2xti/ChZ/j2KGhb8Gk+TIa+Pv8=";
    };

    buildPhase = ''
      runHook preBuild
      pnpm run build
      runHook postBuild
    '';

    # vite emits to ../static/dist.
    installPhase = ''
      runHook preInstall
      cp -r ../static/dist $out
      runHook postInstall
    '';
  });
in
buildGoModule {
  pname = "mo";
  inherit version src;

  vendorHash = "sha256-rmtJswO3DWWxpb2uk91aIatc7ugNmsqzwlEeKdX7ITE=";

  preBuild = ''
    cp -r ${frontend} internal/static/dist
  '';

  ldflags = [
    "-s"
    "-w"
    "-X github.com/k1LoW/mo/version.Revision=v${version}"
  ];

  meta = {
    description = "Markdown viewer that opens .md files in a browser";
    homepage = "https://github.com/k1LoW/mo";
    changelog = "https://github.com/k1LoW/mo/raw/v${version}/CHANGELOG.md";
    license = lib.licenses.mit;
    mainProgram = "mo";
  };
}