summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 7430f962a38d2f7d883c6eb6a3f3102776643424 (plain) (blame)
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
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

    flake-parts = {
      url = "github:hercules-ci/flake-parts";
      inputs.nixpkgs-lib.follows = "nixpkgs";
    };

    flake-utils.url = "github:numtide/flake-utils";

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs@{ flake-parts, flake-utils, crane, nixpkgs, ... }:
    let
      # Generate the typst package for the given nixpkgs instance.
      packageFor = pkgs:
        let
          inherit (nixpkgs.lib)
            importTOML
            optionals
            sourceByRegex
            ;
          Cargo-toml = importTOML ./Cargo.toml;

          pname = "typst";
          version = Cargo-toml.workspace.package.version;

          # Crane-based Nix flake configuration.
          # Based on https://github.com/ipetkov/crane/blob/master/examples/trunk-workspace/flake.nix

          craneLib = crane.mkLib pkgs;

          # Typst files to include in the derivation.
          # Here we include Rust files, assets and tests.
          src = sourceByRegex ./. [
            "(assets|crates|tests)(/.*)?"
            ''Cargo\.(toml|lock)''
            ''build\.rs''
          ];

          # Typst derivation's args, used within crane's derivation generation
          # functions.
          commonCraneArgs = {
            inherit src pname version;

            buildInputs = optionals pkgs.stdenv.isDarwin [
              pkgs.darwin.apple_sdk.frameworks.CoreServices
            ];

            nativeBuildInputs = [ pkgs.installShellFiles ];
          };

          # Derivation with just the dependencies, so we don't have to keep
          # re-building them.
          cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs;

          typst = craneLib.buildPackage (commonCraneArgs // {
            inherit cargoArtifacts;

            postInstall = ''
              installManPage crates/typst-cli/artifacts/*.1
              installShellCompletion \
                crates/typst-cli/artifacts/typst.{bash,fish} \
                --zsh crates/typst-cli/artifacts/_typst
            '';

            GEN_ARTIFACTS = "artifacts";
          });
        in
        typst;
    in
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [
        "aarch64-darwin"
        "aarch64-linux"
        "x86_64-darwin"
        "x86_64-linux"
      ];

      flake = {
        overlays.default = _: prev: {
          typst-dev = packageFor prev;
        };
      };

      perSystem = { pkgs, ... }:
        let
          inherit (pkgs) lib;
          typst = packageFor pkgs;
        in
        {
          packages.default = typst;

          apps.default = flake-utils.lib.mkApp {
            drv = typst;
          };

          devShells.default = pkgs.mkShell {
            packages = with pkgs; [
              rustc
              cargo
            ];

            buildInputs = lib.optionals pkgs.stdenv.isDarwin [
              pkgs.darwin.apple_sdk.frameworks.CoreServices
              pkgs.libiconv
            ];
          };
        };
    };
}