aboutsummaryrefslogtreecommitdiff
path: root/default.nix
blob: f9afabe966e05ef3acf93dbc3206d7dc77a483f7 (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
98
99
100
101
# This Nix file can be handy when working on the github version of pandoc.
#
# To build pandoc run:
#    nix-build
# To run a nix shell with everthing needed to build pandoc with cabal:
#    nix-shell
# To build pandoc for use on Linux and macOS systems without Nix:
#    nix-build -A patched
{ pkgs ?
    import ((import <nixpkgs> {}).pkgs.fetchFromGitHub {
      owner = "NixOS"; repo = "nixpkgs";
      rev = "1354099daf98b7a1f79e6c41ce6bfda5c40177ae";
      sha256 = "1dwnmxirzjrshlaan7cpag77y7nd09chrbakfx3c3f1lzsldbi97";
    }) {} }:
let haskellPackages = pkgs.haskellPackages;
    overrides = self: super: {
      hslua-module-text = pkgs.haskell.lib.dontCheck
            super.hslua-module-text;
      skylighting = super.skylighting_0_5;
      hslua = super.hslua_0_9_3;
    };
    source-overrides = {
      doctemplates = "0.2.1";
      texmath = "0.10";
      pandoc-types = pkgs.fetchFromGitHub {
        owner = "jgm";
        repo = "pandoc-types";
        rev = "f1278603a4766f32b8375de84b8581f4bb1e665a";
        sha256 = "05ykcs8qdjrxly9b6chjr939mv6r42mh51bl58k7jxsr1crxrrf9";
      };
    };
    filterHaskellSource = src:
      builtins.filterSource (path: type:
        pkgs.lib.all (i: i != baseNameOf path) [ ".git" "dist-newstyle" "cabal.project.local" "dist" ".stack-work" ".DS_Store" "default.nix" "result" ]
          && pkgs.lib.all (i: !(pkgs.lib.hasSuffix i path)) [ ".lkshf" ]
          && pkgs.lib.all (i: !(pkgs.lib.hasPrefix i path)) [ ".ghc.environment." ]
        ) src;

    # Normal nix derivation
    drv = (
        haskellPackages.extend (
          pkgs.lib.composeExtensions (
            haskellPackages.packageSourceOverrides source-overrides
          ) overrides
        )
      ).callCabal2nix "pandoc" (filterHaskellSource ./.) {};

    # Like drv but with static linking for haskell libraries
    static = pkgs.haskell.lib.justStaticExecutables (drv.overrideAttrs (old: {
        buildInputs = old.buildInputs ++ [
          pkgs.zlib.static
          haskellPackages.file-embed
        ];
        configureFlags = [
          "-fembed_data_files"
          "--disable-executable-dynamic"
        ];
      }));

    # Patch binaries for use on macOS and linux systems without nix
    # and bundle the required gmp and lua libraries
    patched = pkgs.stdenv.mkDerivation {
        name = "pandoc-patched";
        buildInputs = [
          static
          pkgs.zip
          pkgs.gnutar
        ];
        unpackPhase = "true";
        buildPhase = "true";
        installPhase = if pkgs.stdenv.isDarwin
          then ''
              mkdir -p $out/bin
              cp ${static}/bin/pandoc $out/bin
              cp ${pkgs.gmp}/lib/libgmp.10.dylib $out/bin
              cp ${pkgs.lua5_3}/lib/liblua.5.3.4.dylib $out/bin
              chmod +w $out/bin/*
              echo patching libgmp and liblua
              install_name_tool -id "@executable_path/libgmp.10.dylib" "$out/bin/libgmp.10.dylib"
              install_name_tool -id "@executable_path/liblua.5.3.4.dylib" "$out/bin/liblua.5.3.4.dylib"
              for fn in $out/bin/*; do
                echo patching $fn
                install_name_tool -change "${pkgs.libiconv}/lib/libiconv.dylib" /usr/lib/libiconv.dylib "$fn"
                install_name_tool -change "${pkgs.stdenv.libc}/lib/libSystem.B.dylib" /usr/lib/libSystem.B.dylib "$fn"
                install_name_tool -change "${pkgs.gmp}/lib/libgmp.10.dylib" "@executable_path/libgmp.10.dylib" "$fn"
                install_name_tool -change "${pkgs.lua5_3}/lib/liblua.5.3.4.dylib" "@executable_path/liblua.5.3.4.dylib" "$fn"
              done
              (cd $out/.. && zip -r $out/pandoc-macOS.zip `basename $out`/bin)
            ''
          else ''
              mkdir -p $out/bin
              cp ${static}/bin/pandoc $out/bin
              cp ${pkgs.gmp}/lib/libgmp.so* $out/bin
              cp ${pkgs.lua5_3}/lib/liblua.so* $out/bin
              chmod +w $out/bin/pandoc
              patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 $out/bin/pandoc
              patchelf --set-rpath '$ORIGIN' $out/bin/pandoc
              (cd $out/.. && tar -czf $out/pandoc-linux.tar.gz `basename $out`/bin)
            '';
    };
in if pkgs.lib.inNixShell then drv.env else drv // { inherit static patched; }