summaryrefslogtreecommitdiff
path: root/vere/flake.nix
blob: 05237289b38fa2c6f267a84015161d728e951dda (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
  description = "Vere build devshell";

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

  outputs = inputs@{ self, nixpkgs, parts }: parts.lib.mkFlake { inherit inputs; } (let
    # map systems to the musl-cross-make target names; only
    # x86_64-linux is tested though
    toolchainTargets = {
      "x86_64-linux" = "x86_64-linux-musl";
      "aarch64-linux" = "aarch64-linux-musl";
    };
    # map dep urls to hashes
    toolchainDeps = {
      "https://ftpmirror.gnu.org/gnu/gcc/gcc-9.4.0/gcc-9.4.0.tar.xz" = "13l3p6g2krilaawbapmn9zmmrh3zdwc36mfr3msxfy038hps6pf9";
      "https://ftpmirror.gnu.org/gnu/binutils/binutils-2.33.1.tar.xz" = "1grcf8jaw3i0bk6f9xfzxw3qfgmn6fgkr108isdkbh1y3hnzqrmb";
      "https://musl.libc.org/releases/musl-1.2.3.tar.gz" = "196lrzw0qy5axiz9p5ay50q2mls8hbfckr4rw0klc7jjc9h0nnvx";
      "https://ftpmirror.gnu.org/gnu/gmp/gmp-6.1.2.tar.bz2" = "1clg7pbpk6qwxj5b2mw0pghzawp2qlm3jf9gdd8i6fl6yh2bnxaj";
      "https://ftpmirror.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz" = "0biwnhjm3rx3hc0rfpvyniky4lpzsvdcwhmcn7f0h4iw2hwcb1b9";
      "https://ftpmirror.gnu.org/gnu/mpfr/mpfr-4.0.2.tar.bz2" = "1k1s4p56272bggvyrxfn3zdycr4wy7h5ipac70cr03lys013ypn0";
      "http://ftp.barfooze.de/pub/sabotage/tarballs//linux-headers-4.19.88-1.tar.xz" = "04r8k4ckqbklx9sfm07cr7vfw5ra4cic0rzanm9dfh0crxncfnwr";
      "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=3d5db9ebe860" = "75d5d255a2a273b6e651f82eecfabf6cbcd8eaeae70e86b417384c8f4a58d8d3";
    };
  in {
    systems = builtins.attrNames toolchainTargets;

    flake = {};

    perSystem = { pkgs, system, ... }: let
      target = toolchainTargets.${system};

      # in order to make the toolchain derivation pure, spoof `wget`
      # to use FODs for the dependencies that would otherwise be
      # downloaded
      pseudoWget = let
        c = pkgs.coreutils;
      in pkgs.writeScriptBin "wget" ''
        #!${pkgs.stdenv.shell}

        declare -A targets
        ${builtins.concatStringsSep "\n"
          (builtins.map (url:
            "targets[${nixpkgs.lib.escapeShellArg url}]=" + (nixpkgs.lib.escapeShellArg (pkgs.fetchurl {
              inherit url;
              sha256 = builtins.getAttr url toolchainDeps;
            })))
            (builtins.attrNames toolchainDeps))}

        # -c -O target url
        target="$3"
        url="$4"
        preFetched=${"$" + "{targets[$url]}"}
        if [[ -z "$preFetched" ]]; then
          ${c}/bin/echo 1>&2 "$target ($url) is new or changed, nix-prefetch-url it and add or update the url and hash in toolchainDeps in flake.nix"
          exit 1
        fi
        ${c}/bin/cp --reflink=auto "$preFetched" "$target"
      '';

      toolchain = let
        # keep in sync with `_musl_cross_make_version` in
        # bazel/toolchain/BUILD.bazel
        version = "fe915821b652a7fa37b34a596f47d8e20bc72338";

        # `-g -O2` are the defaults (there's no way to add cflags via
        # config.mak), `-Wno-format-security` is the interesting
        # addition (without which gcc build fails in several places
        # due to `-Werror=format-security` being on for some reason)
        commonCFLAGS = "-g -O2 -Wno-format-security";
      in pkgs.stdenv.mkDerivation {
        name = "musl-cross-make";
        inherit system version;
        src = pkgs.fetchFromGitHub {
          owner = "richfelker";
          repo = "musl-cross-make";
          rev = version;
          sha256 = "sha256-FthfhZ+qGf2nWLICvjaO8fiP5+PYU7PqFCbPwXmJFes=";
        };

        nativeBuildInputs = [
          pseudoWget
        ];

        enableParallelBuilding = true;
        configurePhase = ''
          cat > config.mak <<EOF
          TARGET = ${target}
          OUTPUT = $out
          COMMON_CONFIG += CFLAGS="${commonCFLAGS}" CXXFLAGS="${commonCFLAGS}"
          EOF
        '';
      };
    in {
      devShells.default = (pkgs.buildFHSUserEnvBubblewrap {
        name = "vere-devenv";
        targetPkgs = pkgs: [
          toolchain
        ]
        ++ (with pkgs; [
          autoconf
          autoconf-archive
          automake
          bazel_6
          binutils # for `nm`
          jdk11_headless
          libtool
          m4
          pkg-config
          git
          perl
        ]);
        extraBuildCommands = ''
          chmod +w usr
          mkdir -p usr/local
          # symlinking breaks Bazel inclusion checks, so have to copy
          cp -a --reflink=auto ${toolchain} usr/local/${target}
        '';
      }).env;
    };
  });
}