Skip to content

Instantly share code, notes, and snippets.

@andreabedini
Last active February 21, 2024 04:57
Show Gist options
  • Save andreabedini/d726c191fd7b6eb93dd954115d679547 to your computer and use it in GitHub Desktop.
Save andreabedini/d726c191fd7b6eb93dd954115d679547 to your computer and use it in GitHub Desktop.
{
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
httpStoreServer = import ./httpStoreServer.nix { inherit pkgs; };
mkHackage = import ./mkHackage.nix { inherit pkgs; };
inputMap = {
"hackage.haskell.org" = mkHackage {
url =
"https://web.archive.org/web/20230113014059/https://hackage.haskell.org/01-index.tar.gz";
sha256 = "0l6qsivvzs1m2a0zfjdc8qwh8n5rs5f4wzslmwnv8gsj3x6sj2qi";
# it's a bit annoying that we have to hardcode this but as we can know the hash we can know the file size
length = 111046817;
};
};
thing = pkgs.runCommand "thing" {
nativeBuildInputs = with pkgs; [
cabal-install
ghc
curl
(httpStoreServer inputMap)
];
} ''
mkdir -p $out
export CABAL_DIR=$out
cat >$CABAL_DIR/config <<EOF
repository hackage.haskell.org
url: http://hackage.haskell.org/
secure: True
root-keys: aaa
key-threshold: 0
EOF
cabal update -v3
'';
in { packages.default = thing; });
}
{ pkgs }:
inputMap:
let
caddyConfig = pkgs.writeTextFile {
name = "caddy-config";
text = builtins.toJSON {
admin.disabled = true;
apps.http.servers.srv0 = {
automatic_https.disable = true;
listen = [ "unix//tmp/caddy" ];
routes = pkgs.lib.mapAttrsToList
(host: path: {
match = [{ host = [ host ]; }];
handle = [{
handler = "file_server";
root = path;
}];
terminal = true;
})
inputMap;
};
};
};
in
pkgs.writeTextFile {
name = "httpPathsServer";
destination = "/nix-support/setup-hook";
text = ''
set -e
if [[ ! -v CURL_HOME || ! -e "$CURL_HOME/.curlrc" ]]; then
HOME=$(mktemp -d) ${pkgs.caddy}/bin/caddy start --config ${caddyConfig}
export CURL_HOME=$(mktemp -d)
echo "unix-socket /tmp/caddy" > $CURL_HOME/.curlrc
fi
'';
}
{ pkgs }:
{ url, sha256, length }:
let
index = builtins.fetchurl {
name = "hackage-haskell-org-01-index.tar.gz";
inherit url sha256;
};
root = builtins.toJSON {
signatures = [ ];
signed = {
_type = "Root";
expires = null;
keys = { };
roles = {
mirrors = {
keyids = [ ];
threshold = 0;
};
root = {
keyids = [ ];
threshold = 0;
};
snapshot = {
keyids = [ ];
threshold = 0;
};
targets = {
keyids = [ ];
threshold = 0;
};
timestamp = {
keyids = [ ];
threshold = 0;
};
};
version = 1;
};
};
timestamp = builtins.toJSON {
signatures = [ ];
signed = {
_type = "Timestamp";
expires = null;
meta = {
"<repo>/snapshot.json" = {
hashes = {
md5 = builtins.hashString "md5" snapshot;
sha256 = builtins.hashString "sha256" snapshot;
};
length = builtins.stringLength snapshot;
};
};
version = 1;
};
};
snapshot = builtins.toJSON {
signatures = [ ];
signed = {
_type = "Snapshot";
expires = null;
meta = {
"<repo>/01-index.tar.gz" = {
hashes = {
md5 = builtins.hashFile "md5" index;
sha256 = builtins.hashFile "sha256" index;
};
length = length;
};
"<repo>/root.json" = {
hashes = {
md5 = builtins.hashString "md5" root;
sha256 = builtins.hashString "sha256" root;
};
length = builtins.stringLength root;
};
"<repo>/mirrors.json" = {
hashes = {
md5 = builtins.hashString "md5" mirrors;
sha256 = builtins.hashString "sha256" mirrors;
};
length = builtins.stringLength mirrors;
};
};
version = 1;
};
};
mirrors = builtins.toJSON {
signatures = [ ];
signed = {
_type = "Mirrorlist";
expires = null;
mirrors = [ ];
version = 1;
};
};
in pkgs.runCommand "hackage.haskell.org" { } ''
mkdir $out
ln -s ${index} $out/01-index.tar.gz
echo -n '${root}' > $out/root.json
echo -n '${timestamp}' > $out/timestamp.json
echo -n '${snapshot}' > $out/snapshot.json
echo -n '${mirrors}' > $out/mirrors.json
''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment