chore: Implement code review suggestions

This commit is contained in:
Piotr Gaczkowski 2026-04-13 10:53:07 +02:00
parent 3fe23fdc38
commit e0eb0b4617
9 changed files with 203 additions and 14 deletions

4
.envrc
View file

@ -1,12 +1,14 @@
#!/usr/bin/env bash
# Install nix_direnv for better caching and handling of nix configuration
if ! has nix_direnv_version || ! nix_direnv_version 3.0.5; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.5/direnvrc" "sha256-RuwIS+QKFj/T9M2TFXScjBsLR6V3A17YVoEW/Q6AZ1w="
fi
watch_file nix/*.nix
# shell.nix allows using devenv on nix systems that don't have devenv installed
use nix
export DIRENV_WARN_TIMEOUT=20s
export DIRENV_WARN_TIMEOUT=30s
eval "$(devenv direnvrc)"

3
.gitignore vendored
View file

@ -6,4 +6,5 @@ __pycache__/
*.egg-info/
.env*
*.pickle
src/kronos/*events*.html
dist/
result

View file

@ -6,6 +6,12 @@ Share events between services.
Nix is required.
- Add cachix binary cache (this will speed up building):
```bash
cachix use hs3city
```
- Set up environment.
```bash

View file

@ -2,14 +2,21 @@
{
name = "kronos";
languages.python = {
enable = true;
venv.enable = true;
version = "3.13.0";
uv = {
cachix = {
pull = [ "hs3city" ];
push = "hs3city";
};
languages = {
python = {
enable = true;
sync.enable = true;
sync.allExtras = true;
venv.enable = true;
version = "3.13.0";
uv = {
enable = true;
sync.enable = true;
sync.allExtras = true;
};
};
};

146
npins/default.nix Normal file
View file

@ -0,0 +1,146 @@
/*
This file is provided under the MIT licence:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
# Generated by npins. Do not modify; will be overwritten regularly
let
data = builtins.fromJSON (builtins.readFile ./sources.json);
version = data.version;
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range =
first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";
# If the environment variable NPINS_OVERRIDE_${name} is set, then use
# the path directly as opposed to the fetched source.
# (Taken from Niv for compatibility)
mayOverride =
name: path:
let
envVarName = "NPINS_OVERRIDE_${saneName}";
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
ersatz = builtins.getEnv envVarName;
in
if ersatz == "" then
path
else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
if builtins.substring 0 1 ersatz == "/" then
/. + ersatz
else
/. + builtins.getEnv "PWD" + "/${ersatz}"
);
mkSource =
name: spec:
assert spec ? type;
let
path =
if spec.type == "Git" then
mkGitSource spec
else if spec.type == "GitRelease" then
mkGitSource spec
else if spec.type == "PyPi" then
mkPyPiSource spec
else if spec.type == "Channel" then
mkChannelSource spec
else if spec.type == "Tarball" then
mkTarballSource spec
else
builtins.throw "Unknown source type ${spec.type}";
in
spec // { outPath = mayOverride name path; };
mkGitSource =
{
repository,
revision,
url ? null,
submodules,
hash,
branch ? null,
...
}:
assert repository ? type;
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
# In the latter case, there we will always be an url to the tarball
if url != null && !submodules then
builtins.fetchTarball {
inherit url;
sha256 = hash; # FIXME: check nix version & use SRI hashes
}
else
let
url =
if repository.type == "Git" then
repository.url
else if repository.type == "GitHub" then
"https://github.com/${repository.owner}/${repository.repo}.git"
else if repository.type == "GitLab" then
"${repository.server}/${repository.repo_path}.git"
else
throw "Unrecognized repository type ${repository.type}";
urlToName =
url: rev:
let
matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
short = builtins.substring 0 7 rev;
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
in
"${if matched == null then "source" else builtins.head matched}${appendShort}";
name = urlToName url revision;
in
builtins.fetchGit {
rev = revision;
inherit name;
# hash = hash;
inherit url submodules;
};
mkPyPiSource =
{ url, hash, ... }:
builtins.fetchurl {
inherit url;
sha256 = hash;
};
mkChannelSource =
{ url, hash, ... }:
builtins.fetchTarball {
inherit url;
sha256 = hash;
};
mkTarballSource =
{
url,
locked_url ? url,
hash,
...
}:
builtins.fetchTarball {
url = locked_url;
sha256 = hash;
};
in
if version == 5 then
builtins.mapAttrs mkSource data.pins
else
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"

18
npins/sources.json Normal file
View file

@ -0,0 +1,18 @@
{
"pins": {
"nixpkgs": {
"type": "Git",
"repository": {
"type": "GitHub",
"owner": "nixos",
"repo": "nixpkgs"
},
"branch": "nixos-25.11",
"submodules": false,
"revision": "54170c54449ea4d6725efd30d719c5e505f1c10e",
"url": "https://github.com/nixos/nixpkgs/archive/54170c54449ea4d6725efd30d719c5e505f1c10e.tar.gz",
"hash": "0ayx6v3wwvqpdjkbfnbgyp7rwnsypgn6willidza61x9ilmxkqdp"
}
},
"version": 5
}

View file

@ -1,6 +1,15 @@
# Use npins to pin nixpkgs dependencies
# This makes it easier to cache build results
{
pkgs ? import <nixpkgs> { },
sources ? import ./npins,
system ? builtins.currentSystem,
pkgs ? import sources.nixpkgs {
inherit system;
config = { };
overlays = [ ];
},
}:
# Create a nix shell with devenv (so `use devenv` can work in direnv/.envrc)
pkgs.mkShell {
buildInputs = with pkgs; [
devenv

View file

@ -222,17 +222,17 @@ def _main():
events = prepare_events_for_template(raw_events)
script_dir = os.path.dirname(os.path.abspath(__file__))
os.makedirs("dist", exist_ok=True)
output_path = os.path.join(os.getcwd(), "dist", OUTPUT_FILENAME)
html_output = render_newsletter(
events=events, template_dir=script_dir, template_name=TEMPLATE_FILENAME
events=events, template_dir=os.getcwd(), template_name=TEMPLATE_FILENAME
)
output_path = os.path.join(script_dir, OUTPUT_FILENAME)
with open(output_path, "w", encoding="utf-8") as f:
f.write(html_output)
print(f"File '{output_path}' generated successfully.", file=sys.stderr)
inlined_output_path = os.path.join(script_dir, "newsletter_events_inlined.html")
inlined_output_path = os.path.join(os.getcwd(), "dist", "newsletter_events_inlined.html")
inline_css(output_path, inlined_output_path)
print(
f"File '{inlined_output_path}' (inline CSS) generated.",