Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tooling): Fix Nix buld for rustier sile #1871

Merged
merged 9 commits into from
Oct 6, 2023
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,7 @@ completions/
core/pathsetup.lua

# Nix symlink to builds
result/
result
result-man
result-doc
result-dev
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 14 additions & 82 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,7 @@
inherit (gitignore.lib) gitignoreSource;
# https://discourse.nixos.org/t/passing-git-commit-hash-and-tag-to-build-with-flakes/11355/2
version_rev = if (self ? rev) then (builtins.substring 0 7 self.rev) else "dirty";
# Prepare a different luaEnv to be used in the overridden expression,
# this is also the place to choose a different lua interpreter, such as
# lua5_4 or luajit
luaEnv = pkgs.lua5_3.withPackages(ps: with ps; [
cassowary
cldr
fluent
linenoise
loadkit
lpeg
lua-zlib
lua_cliargs
luaepnf
luaexpat
luafilesystem
luarepl
luasec
luasocket
luautf8
penlight
vstruct
# lua packages needed for testing
busted
luacheck
# If we want to test things with lua5.2 or an even older lua, we uncomment these
#bit32
#compat53
]);
# Use the expression from Nixpkgs instead of rewriting it here.
sile = pkgs.sile.overrideAttrs(oldAttr: rec {
sile = pkgs.callPackage ./pkg.nix {
version = "${(pkgs.lib.importJSON ./package.json).version}-${version_rev}-flake";
src = pkgs.lib.cleanSourceWith {
# Ignore many files that gitignoreSource doesn't ignore, see:
Expand Down Expand Up @@ -95,62 +66,23 @@
]);
src = gitignoreSource ./.;
};
# Add the libtexpdf src instead of the git submodule.
# Also pretend to be a tarball release so sile --version will not say `vUNKNOWN`.
preAutoreconf = ''
rm -rf ./libtexpdf
# From some reason without this flag, libtexpdf/ is unwriteable
cp --no-preserve=mode -r ${libtexpdf-src} ./libtexpdf/
echo ${version} > .tarball-version
'';
# Don't build the manual as it's time consuming, and it requires fonts
# that are not available in the sandbox due to internet connection
# missing.
configureFlags = [
"PDFINFO=false"
] ++ (
pkgs.lib.lists.remove "--with-manual" oldAttr.configureFlags
);
nativeBuildInputs = oldAttr.nativeBuildInputs ++ [
pkgs.autoreconfHook
];
buildInputs = [
# Build inputs added since release in nixpkgs
pkgs.cargo
pkgs.jq
pkgs.rustc
] ++ [
# Add here inputs needed for development, and not for Nixpkgs' build.
pkgs.libarchive
pkgs.perl
# This line, along with the `pkgs.list.drop 1` line afterwards,
# replaces the luaEnv originated in `oldAttr.buildInputs`.
luaEnv
] ++ (
# Add all buildInputs from Nixpkgs' derivation, besides the 1st
# one, which is Nixpkgs' luaEnv. NOTE it's not mandatory to `drop`
# the first buildInput of `oldAttr` as so, because the first `lua`
# interpreter that would have been found otherwise would have been
# the one belonging to the first `luaEnv` of the final
# `buildInputs`. However, we'd like to keep the `buildInputs` clean
# never the less.
pkgs.lib.lists.drop 1 oldAttr.buildInputs
);
meta = oldAttr.meta // {
changelog = "https://github.com/sile-typesetter/sile/raw/master/CHANGELOG.md";
};
});
inherit libtexpdf-src;
};
inherit (sile.passthru) luaEnv;
in rec {
devShells = {
default = pkgs.mkShell {
inherit (sile) checkInputs buildInputs FONTCONFIG_FILE;
inherit (sile)
buildInputs
nativeCheckInputs
FONTCONFIG_FILE
;
configureFlags = sile.configureFlags ++ [ "--enable-developer" ];
nativeBuildInputs = sile.nativeBuildInputs ++ [ pkgs.luarocks-nix ];
# This is written in Nixpkgs' expression as well, but we need to write
# this here so that the overridden luaEnv will be used instead.
passthru = {
inherit luaEnv;
};
nativeBuildInputs = sile.nativeBuildInputs ++ [
pkgs.luarocks
# For commitlint git hook
pkgs.yarn
];
};
};
packages.sile = sile;
Expand Down
173 changes: 173 additions & 0 deletions pkg.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# NOTE: This file is supposed to be similar to what is in Nixpkgs, except for
# the `version`, `src` and `libtexpdf-src` attributes that are given by the
# `flake.nix`. In Nixpkgs, we don't need `libtexpdf-src` because we use
# `fetchFromGitHub` with fetchSubmodules = true;`.
{ lib
, stdenv
, version, src, libtexpdf-src
, autoreconfHook
, gitMinimal
, pkg-config
, jq
, cargo
, rustc
, rustPlatform
, makeWrapper
, poppler_utils
, harfbuzz
, icu
, fontconfig
, lua
, libiconv
, darwin
, makeFontsConf
, gentium
, runCommand
}:

let
luaEnv = lua.withPackages(ps: with ps; [
alerque marked this conversation as resolved.
Show resolved Hide resolved
cassowary
cldr
cosmo
fluent
linenoise
loadkit
lpeg
lua-zlib
lua_cliargs
luaepnf
luaexpat
luafilesystem
luarepl
luasec
luasocket
luautf8
penlight
vstruct
# lua packages needed for testing
busted
luacheck
# NOTE: Add lua packages here, to change the luaEnv also read by `flake.nix`
] ++ lib.optionals (lib.versionOlder lua.luaversion "5.2") [
bit32
] ++ lib.optionals (lib.versionOlder lua.luaversion "5.3") [
compat53
]);
in stdenv.mkDerivation (finalAttrs: {
pname = "sile";
inherit version src;

preAutoreconf = ''
# Add the libtexpdf src instead of the git submodule. (From some reason
# without --no-preserve=mode flag, libtexpdf/ is unwriteable). As explained
# before, in Nixpkgs, we won't need to run these commands.
rm -rf ./libtexpdf
cp --no-preserve=mode -r ${libtexpdf-src} ./libtexpdf/
# pretend to be a tarball release so sile --version will not say `vUNKNOWN`.
echo ${finalAttrs.version} > .tarball-version
'';

nativeBuildInputs = [
autoreconfHook
gitMinimal
pkg-config
jq
cargo
rustc
rustPlatform.cargoSetupHook
poppler_utils
makeWrapper
];
# In Nixpkgs, we don't copy the Cargo.lock file from the repo to Nixpkgs'
# repo, but we inherit src, and specify a hash (it is a fixed output
# derivation). `nix-update` and `nixpkgs-update` should be able to catch that
# hash and update it as well when performing updates.
cargoDeps = rustPlatform.importCargoLock {
lockFile = ./Cargo.lock;
};

buildInputs = [
luaEnv
harfbuzz
icu
fontconfig
libiconv
] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.AppKit
];

configureFlags = [
"--with-system-luarocks"
# TODO: Explain this flag
alerque marked this conversation as resolved.
Show resolved Hide resolved
"PDFINFO=false"
#"--with-manual" In Nixpkgs we add this flag, here its not important enough
doronbehar marked this conversation as resolved.
Show resolved Hide resolved
];

postPatch = ''
patchShebangs build-aux/*.sh
'' + lib.optionalString stdenv.isDarwin ''
sed -i -e 's|@import AppKit;|#import <AppKit/AppKit.h>|' src/macfonts.m
alerque marked this conversation as resolved.
Show resolved Hide resolved
'';

NIX_LDFLAGS = lib.optionalString stdenv.isDarwin "-framework AppKit";
alerque marked this conversation as resolved.
Show resolved Hide resolved

FONTCONFIG_FILE = makeFontsConf {
fontDirectories = [
gentium
];
};

enableParallelBuilding = true;

preBuild = lib.optionalString stdenv.cc.isClang ''
substituteInPlace libtexpdf/dpxutil.c \
--replace "ASSERT(ht && ht->table && iter);" "ASSERT(ht && iter);"
alerque marked this conversation as resolved.
Show resolved Hide resolved
'';

# remove forbidden references to $TMPDIR
preFixup = lib.optionalString stdenv.isLinux ''
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one on the other hand a rather common thing you see in Nixpkgs, but we should verify it is still needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would I confirm if this is needed? Would the build fail with a warning about it if there was a problem? Because just dropping this block entirely seems to allow the Flake to continue running just fine. See #1941.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would I confirm if this is needed?

I'm pretty sure that if you remove it and the Nix build succeeds, it should be OK.

for f in "$out"/bin/*; do
if isELF "$f"; then
patchelf --shrink-rpath --allowed-rpath-prefixes "$NIX_STORE" "$f"
fi
done
'';

passthru = {
# So it will be easier to inspect this environment, in comparison to others
inherit luaEnv;
# Copied from Makefile.am
tests.test = lib.optionalAttrs (!(stdenv.isDarwin && stdenv.isAarch64)) (
runCommand "${finalAttrs.pname}-test" {
nativeBuildInputs = [ poppler_utils finalAttrs.finalPackage ];
inherit (finalAttrs) FONTCONFIG_FILE;
} ''
output=$(mktemp -t selfcheck-XXXXXX.pdf)
echo "<sile>foo</sile>" | sile -o $output -
pdfinfo $output | grep "SILE v${finalAttrs.version}" > $out
'');
};

outputs = [ "out" "doc" "man" "dev" ];

meta = {
description = "A typesetting system";
longDescription = ''
SILE is a typesetting system; its job is to produce beautiful
printed documents. Conceptually, SILE is similar to TeX—from
which it borrows some concepts and even syntax and
algorithms—but the similarities end there. Rather than being a
derivative of the TeX family SILE is a new typesetting and
layout engine written from the ground up using modern
technologies and borrowing some ideas from graphical systems
such as InDesign.
'';
homepage = "https://sile-typesetter.org";
# In nixpkgs we use a version specific URL for CHANGELOG.md
changelog = "https://github.com/sile-typesetter/sile/raw/master/CHANGELOG.md";
platforms = lib.platforms.unix;
maintainers = with lib.maintainers; [ doronbehar alerque ];
license = lib.licenses.mit;
};
})
Loading