Skip to content

Commit

Permalink
Merge branch 'unstable'
Browse files Browse the repository at this point in the history
  • Loading branch information
SFrijters committed Jul 27, 2024
2 parents eb59008 + c25f793 commit 51e8010
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 107 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/nix-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v22
- uses: cachix/install-nix-action@v27
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: DeterminateSystems/magic-nix-cache-action@main
- run: nix run . -- --version
- uses: DeterminateSystems/magic-nix-cache-action@v6
- run: |
nix run . -- --version
nix flake check -L
nix fmt
git diff --exit-code
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This flake provides a NixOS module for [logwatch](https://sourceforge.net/projec
It probably does not cover all use cases, as it has been lifted out of my personal configuration.
Any suggestions or PR are welcome!

The `master` branch of this repository packages [tagged logwatch releases](https://sourceforge.net/p/logwatch/git/ci/7.10/tree/) (starting with 7.10), while the `unstable` branch will be updated more regularly following the [logwatch master branch](https://sourceforge.net/p/logwatch/git/ci/master/tree/).
The `master` branch of this repository packages [tagged logwatch releases](https://sourceforge.net/p/logwatch/git/ci/7.11/tree/) (currently 7.11), while the `unstable` branch will be updated more regularly following the [logwatch master branch](https://sourceforge.net/p/logwatch/git/ci/master/tree/).

## Basic usage

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 53 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,65 @@
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
outputs =
{
self,
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = rec {
default = logwatch;
logwatch = pkgs.callPackage ./packages/logwatch.nix {};
nix-gc-script = pkgs.callPackage ./packages/logwatch-scripts/nix-gc.nix {};
nixos-upgrade-script = pkgs.callPackage ./packages/logwatch-scripts/nixos-upgrade.nix {};
logwatch = pkgs.callPackage ./packages/logwatch.nix { };
nix-gc-script = pkgs.callPackage ./packages/logwatch-scripts/nix-gc.nix { };
nixos-upgrade-script = pkgs.callPackage ./packages/logwatch-scripts/nixos-upgrade.nix { };
};

checks.default = pkgs.nixosTest {
name = "logwatch-module-test";

nodes.server =
{ ... }:
{

imports = [ self.nixosModules.logwatch ];

environment.systemPackages = [ pkgs.mailutils ];

services = {
logwatch.enable = true;
postfix.enable = true;
};
};

testScript = ''
import time
start_all()
server.wait_for_unit("default.target")
# Force restart of logwatch so it sends a mail
server.systemctl("restart logwatch")
# VMs on CI runners can be kind of slow, delay here
time.sleep(3)
# Get all mails for root and check if the expected subject is there
mail = server.succeed("mail -p")
print(mail)
if "Subject: Logwatch for server" not in mail:
raise
if "Logwatch ${self.packages.${system}.logwatch.version}" not in mail:
raise
'';
};

formatter = pkgs.nixfmt-rfc-style;
}
) //
{
)
// {
nixosModules = {
logwatch = import ./modules/logwatch.nix;
};
Expand Down
99 changes: 52 additions & 47 deletions modules/logwatch.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ lib, pkgs, config, ... }:
{
lib,
pkgs,
config,
...
}:
let
# Inspiration from https://git.lamsade.dauphine.fr/tamby/nixos-router/-/blob/master/pkgs/logwatch.nix
# https://sourceforge.net/p/logwatch/discussion/1115929/thread/99b02722/
Expand Down Expand Up @@ -31,53 +36,53 @@ let
'';
};
in
{
options.services.logwatch = {
enable = lib.mkEnableOption "logwatch";
mailto = lib.mkOption {
default = "root";
type = types.str;
description = "Recipient of the reports";
};
range = lib.mkOption {
default = "Yesterday";
type = types.str;
description = "Time range to digest (use logwatch --range Help for details)";
};
detail = lib.mkOption {
default = "Low";
type = types.str;
description = "Detail level of the analysis";
};
service = lib.mkOption {
default = "All";
type = types.str;
description = "Which services to digest";
};
startAt = lib.mkOption {
default = "*-*-* 4:00:00";
type = types.str;
description = "When to run";
};
journalCtlEntries = lib.mkOption {
default = [];
type = types.listOf types.attrs;
description = "What to watch";
};
{
options.services.logwatch = {
enable = lib.mkEnableOption "logwatch";
mailto = lib.mkOption {
default = "root";
type = types.str;
description = "Recipient of the reports";
};
range = lib.mkOption {
default = "Yesterday";
type = types.str;
description = "Time range to digest (use logwatch --range Help for details)";
};
detail = lib.mkOption {
default = "Low";
type = types.str;
description = "Detail level of the analysis";
};
service = lib.mkOption {
default = "All";
type = types.str;
description = "Which services to digest";
};
startAt = lib.mkOption {
default = "*-*-* 4:00:00";
type = types.str;
description = "When to run";
};
journalCtlEntries = lib.mkOption {
default = [ ];
type = types.listOf types.attrs;
description = "What to watch";
};
};

config = lib.mkIf cfg.enable {
environment.systemPackages = [ logwatchWithTemp ];
systemd.services.logwatch = {
description = "Digests the system logs";
wantedBy = [ ];
after = [ "network.target" ];
startAt = "${cfg.startAt}";
serviceConfig = {
Type = "oneshot";
ExecStart = ''${logwatchWithTemp}/bin/logwatch --archives --range "${cfg.range}" --detail "${cfg.detail}" --service "${cfg.service}" --mailto "${cfg.mailto}"'';
PrivateTmp = true;
};
config = lib.mkIf cfg.enable {
environment.systemPackages = [ logwatchWithTemp ];
systemd.services.logwatch = {
description = "Digests the system logs";
wantedBy = [ ];
after = [ "network.target" ];
startAt = "${cfg.startAt}";
serviceConfig = {
Type = "oneshot";
ExecStart = ''${logwatchWithTemp}/bin/logwatch --archives --range "${cfg.range}" --detail "${cfg.detail}" --service "${cfg.service}" --mailto "${cfg.mailto}"'';
PrivateTmp = true;
};
};
}
};
}
14 changes: 11 additions & 3 deletions packages/logwatch-scripts/nixos-upgrade.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
{ pkgs, ...}:
{ pkgs, ... }:

pkgs.writeShellApplication {
name = "logwatch-nixos-upgrade";
text = ''
package_update_regex="^ */nix/store"
keep_output_regex="updating lock file|updated input|github:NixOS/nixpkgs|will be built|will be fetched|^ /nix/store|switching to system configuration|NixOS Upgrade.|error: builder for"
keep_output_regex="updating lock file|will be built|will be fetched|^ /nix/store|switching to system configuration|NixOS Upgrade.|error: builder for"
updated_output_regex="Updated input"
error_regex="error: builder for|Failed to start"
nprint=0
last=
while IFS= read -r line; do
if [[ "''${line}" =~ ''${package_update_regex} ]]; then
if [ "''${nprint}" -gt 0 ]; then
echo "''${line}"
nprint=$((nprint - 1))
elif [[ "''${line}" =~ ''${package_update_regex} ]]; then
# shellcheck disable=SC2001
package_name="$(echo "''${line}" | sed 's| */nix/store/[^\-]*-||')"
printf "%s " "''${package_name}"
last=package
elif [[ "''${line}" =~ ''${updated_output_regex} ]]; then
echo "''${line}"
nprint=2
elif [[ "''${line}" =~ ''${keep_output_regex} ]]; then
if [ "''${last}" = package ]; then
printf "\n"
Expand Down
Loading

0 comments on commit 51e8010

Please sign in to comment.