typst/flake.nix

129 lines
3.6 KiB
Nix
Raw Normal View History

2023-03-27 17:26:08 +03:00
{
inputs = {
2023-09-11 10:57:46 +03:00
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
2023-03-27 23:42:05 +03:00
systems.url = "github:nix-systems/default";
};
2023-09-11 10:57:46 +03:00
2023-11-17 12:39:27 +03:00
outputs = inputs@{ flake-parts, crane, nixpkgs, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [
inputs.flake-parts.flakeModules.easyOverlay
];
perSystem = { self', pkgs, lib, ... }:
let
# Generate the typst package for the given nixpkgs instance.
packageFor = pkgs:
let
inherit (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.
2024-03-03 17:48:51 +03:00
# Here we include Rust files, docs and tests.
src = sourceByRegex ./. [
2024-03-03 17:48:51 +03:00
"(docs|crates|tests)(/.*)?"
''Cargo\.(toml|lock)''
''build\.rs''
2023-09-11 10:57:46 +03:00
];
# 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
pkgs.libiconv
]) ++ [
pkgs.openssl
];
2023-03-27 23:42:05 +03:00
nativeBuildInputs = [
pkgs.installShellFiles
pkgs.pkg-config
pkgs.openssl.dev
];
};
# Derivation with just the dependencies, so we don't have to keep
# re-building them.
cargoArtifacts = craneLib.buildDepsOnly commonCraneArgs;
in
craneLib.buildPackage (commonCraneArgs // {
2023-09-11 10:57:46 +03:00
inherit cargoArtifacts;
2023-03-27 23:42:05 +03:00
2023-09-11 10:57:46 +03:00
postInstall = ''
installManPage crates/typst-cli/artifacts/*.1
installShellCompletion \
crates/typst-cli/artifacts/typst.{bash,fish} \
--zsh crates/typst-cli/artifacts/_typst
'';
2023-09-11 10:57:46 +03:00
GEN_ARTIFACTS = "artifacts";
2023-11-17 12:39:27 +03:00
TYPST_VERSION =
let
rev = self.shortRev or "dirty";
version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).workspace.package.version;
in
"${version} (${rev})";
meta.mainProgram = "typst";
2023-09-11 10:57:46 +03:00
});
typst = packageFor pkgs;
in
{
formatter = pkgs.nixpkgs-fmt;
packages = {
default = typst;
typst-dev = self'.packages.default;
2023-03-27 17:26:08 +03:00
};
overlayAttrs = builtins.removeAttrs self'.packages [ "default" ];
2023-03-27 17:26:08 +03:00
apps.default = {
type = "app";
program = lib.getExe typst;
2023-09-11 10:57:46 +03:00
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
rustc
cargo
];
buildInputs = (lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.libiconv
]) ++ [
pkgs.openssl
];
nativeBuildInputs = [
pkgs.pkg-config
pkgs.openssl.dev
];
};
};
};
2023-03-27 17:26:08 +03:00
}