Skip to content

Commit dde3bbf

Browse files
committed
Add a flake with a NixOS module to generate files
1 parent 5523569 commit dde3bbf

File tree

5 files changed

+143
-21
lines changed

5 files changed

+143
-21
lines changed

flake.lock

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
description = "nixpkgs-swh";
3+
4+
outputs = { self, nixpkgs }:
5+
let
6+
system = "x86_64-linux";
7+
pkgs = import nixpkgs {
8+
system = "x86_64-linux";
9+
overlays = [ self.overlay ];
10+
};
11+
in {
12+
overlay = final: prev: {
13+
nixpkgs-swh-generate = let
14+
binPath = with final; lib.makeBinPath [ curl nix python3 jq pandoc ];
15+
in final.stdenv.mkDerivation {
16+
name = "nixpkgs-swh-generate";
17+
dontUnpack = true;
18+
nativeBuildInputs = [ final.makeWrapper ];
19+
installPhase = ''
20+
mkdir -p $out/bin
21+
cp ${./scripts/generate.sh} $out/bin/nixpkgs-swh-generate
22+
substituteInPlace $out/bin/nixpkgs-swh-generate \
23+
--replace './scripts/swh-urls.nix' '${./scripts/swh-urls.nix}' \
24+
--replace './scripts/add-sri.py' '${./scripts/add-sri.py}' \
25+
--replace './scripts/analyze.py' '${./scripts/analyze.py}' \
26+
--replace '$PWD/scripts/find-tarballs.nix' '${./scripts/find-tarballs.nix}'
27+
wrapProgram $out/bin/nixpkgs-swh-generate \
28+
--prefix PATH : ${binPath}
29+
'';
30+
};
31+
};
32+
33+
packages.x86_64-linux.nixpkgs-swh-generate = pkgs.nixpkgs-swh-generate;
34+
defaultPackage.x86_64-linux = pkgs.nixpkgs-swh-generate;
35+
36+
nixosModules.nixpkgs-swh = { config, pkgs, lib, ... }: let
37+
cfg = config.services.nixpkgs-swh;
38+
dir = "/var/lib/nixpkgs-swh";
39+
in {
40+
options = {
41+
services.nixpkgs-swh = {
42+
enable = lib.mkOption {
43+
type = lib.types.bool;
44+
default = false;
45+
description = ''
46+
Whether to run the nixpkgs-swh service.
47+
'';
48+
};
49+
testing = lib.mkOption {
50+
type = lib.types.bool;
51+
default = false;
52+
description = ''
53+
Whether to only evaluate the hello attribute for testing purpose.
54+
'';
55+
};
56+
fqdn = lib.mkOption {
57+
type = lib.types.str;
58+
description = ''
59+
The Nginx vhost FQDN used to serve built files.
60+
'';
61+
};
62+
};
63+
};
64+
config = {
65+
nixpkgs.overlays = [ self.overlay ];
66+
systemd.services.nixpkgs-swh = pkgs.lib.mkIf cfg.enable {
67+
description = "nixpkgs-swh";
68+
wantedBy = [ "multi-user.target" ];
69+
restartIfChanged = false;
70+
unitConfig.X-StopOnRemoval = false;
71+
# Do it every day
72+
startAt = "*-*-* 00:00:00";
73+
script = ''
74+
${pkgs.nixpkgs-swh-generate}/bin/nixpkgs-swh-generate ${dir} ${if cfg.testing then "true" else "false"} unstable
75+
'';
76+
};
77+
services.nginx.virtualHosts = {
78+
"${cfg.fqdn}" = {
79+
locations."/" = {
80+
root = "${dir}";
81+
extraConfig = ''
82+
autoindex on;
83+
'';
84+
};
85+
};
86+
};
87+
};
88+
};
89+
};
90+
}

scripts/analyze.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import sys
77
import re
8+
import datetime
89
from urllib.parse import urlparse
910

1011
with open(sys.argv[1], "r") as read_file:
@@ -62,17 +63,19 @@
6263
e['file-type'] = v
6364
break
6465

66+
today = datetime.date.today()
6567

6668
readme = """
6769
68-
The file [`sources-{release}-full.json`](https://nix-community.github.io/nixpkgs-swh/sources-{release}-full.json)
70+
The file [`sources-{release}-full.json`](./sources-{release}-full.json)
6971
has been built from the [nixpkgs revision
7072
`{revision}`](https://github.com/NixOS/nixpkgs/tree/{revision}).
7173
This file contains `{sourceNumber}` sources, coming from
7274
`{hostNumber}` different hosts.
7375
74-
The file [`sources-{release}.json`](https://nix-community.github.io/nixpkgs-swh/sources-{release}.json) is a filtered version which only contains archives. This file is consumed by SWH.
76+
The file [`sources-{release}.json`](./sources-{release}.json) is a filtered version which only contains archives. This file is consumed by Software Heritage to fill its archive.
7577
78+
Generated the {today}.
7679
"""
7780

7881
sortedHosts = sorted(hosts.items(), key=lambda h: h[1], reverse=True)
@@ -81,7 +84,8 @@
8184
revision=j['revision'],
8285
release=j['release'],
8386
sourceNumber=len(sources),
84-
hostNumber=len(sortedHosts)))
87+
hostNumber=len(sortedHosts),
88+
today=today))
8589

8690

8791
print("\n#### By host\n")

scripts/generate.sh

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#!/usr/bin/env nix-shell
2-
#!nix-shell -i bash
3-
#!nix-shell -I nixpkgs=./nix
4-
#!nix-shell -p nix git openssh python3 curl jq
1+
#!/usr/bin/env bash
52
set -euo pipefail
63

74

@@ -49,6 +46,8 @@ generate-release() {
4946
--argstr release ${RELEASE} \
5047
--argstr evaluation ${EVAL_ID} \
5148
--argstr timestamp $(date +%s) \
49+
--argstr find-tarballs $PWD/scripts/find-tarballs.nix \
50+
--arg testing $TESTING \
5251
> ${SOURCES_FILE_FULL}
5352

5453
echo "*** Add integrity attribute"
@@ -66,23 +65,28 @@ generate-release() {
6665

6766
generate-readme() {
6867
cat <<EOF > ${DEST_DIR}/README.md
69-
Fill the Software Heritage archive
68+
# Fill the Software Heritage archive
7069
7170
EOF
7271

73-
for i in $@; do
74-
generate-release ${i}
75-
echo "### NixOS \`${i}\`" >> ${DEST_DIR}/README.md
76-
cat ${DEST_DIR}/readme-${i}.md >> ${DEST_DIR}/README.md
77-
echo >> ${DEST_DIR}/README.md
78-
echo >> ${DEST_DIR}/README.md
79-
shift
80-
done
72+
for i in $@; do
73+
generate-release ${i}
74+
echo "### NixOS \`${i}\` release" >> ${DEST_DIR}/README.md
75+
cat ${DEST_DIR}/readme-${i}.md >> ${DEST_DIR}/README.md
76+
echo >> ${DEST_DIR}/README.md
77+
echo >> ${DEST_DIR}/README.md
78+
shift
79+
done
8180
}
8281

83-
8482
DEST_DIR=$1
8583
mkdir -p ${DEST_DIR}
8684
shift
8785

86+
TESTING=$1
87+
shift
88+
8889
generate-readme $@
90+
91+
echo "** Converting README.md to index.html"
92+
pandoc ${DEST_DIR}/README.md > ${DEST_DIR}/index.html

scripts/swh-urls.nix

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
{ revision ? null, release ? null, evaluation ? null, timestamp ? null }:
1+
{ revision ? null, release ? null, evaluation ? null, timestamp ? null, testing ? false, find-tarballs }:
22

33
with builtins;
44
let
55
pkgs = import <nixpkgs> {};
66
mirrors = import <nixpkgs/pkgs/build-support/fetchurl/mirrors.nix>;
77
expr = import <nixpkgs/maintainers/scripts/all-tarballs.nix>;
8-
urls = import ./find-tarballs.nix {expr = expr;};
9-
8+
urls = import find-tarballs {expr = if testing then expr.hello else expr;};
9+
1010
# This is avoid double slashes in urls that make url non valid
1111
concatUrls = a: b: (pkgs.lib.removeSuffix "/" a) + "/" + (pkgs.lib.removePrefix "/" b);
1212

@@ -20,7 +20,7 @@ let
2020
in if isMirrorUrl
2121
then [ url ]
2222
else map (r: concatUrls r path) resolvedUrls;
23-
23+
2424
# Transform the url list to swh format
2525
toSwh = s: {
2626
inherit (s) postFetch outputHashMode outputHashAlgo outputHash;

0 commit comments

Comments
 (0)