Compare commits

...

3 Commits

Author SHA1 Message Date
Lucas Bergman
79bf02e1ff maint: Some Nix flake cleanups
Specifically:

  1. Switch from the nixosModule flake output (which is deprecated) to
     nixosModules.default
  2. Remove "with lib;" it's convenient, but I've gotten feedback when
     upstreaming module changes that that shouldn't be a thing in
     nixpkgs anymore
  3. Add a little type checking to the NixOS module options
  4. Switch the default config to empty (but leave the example)
  5. Use the convention "let cfg = config.services.heracles," a thing
     that's super common in NixOS
2024-02-19 18:06:35 -06:00
Lucas Bergman
e48d63e523 maint: Set formatter Nix flake output so nix fmt works
I've idly thought about making the formatter a derivation that runs alejandra
over Nix code, then rustfmt over Rust code, etc--so that just running `nix
fmt` in the root cleans up everything--but I haven't made that work yet.
2024-02-19 18:05:35 -06:00
Lucas Bergman
e2c57665d9 maint: Run nix stuff through the formatter
There are several choices for formatting Nix code, but IMHO alejandra is the
right kind of opinionated: <https://github.com/kamadorueda/alejandra>.
2024-02-19 17:47:25 -06:00
2 changed files with 120 additions and 138 deletions

View File

@ -1,11 +1,12 @@
let let
lock = builtins.fromJSON (builtins.readFile ./flake.lock); lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in in
(import ( (import (
fetchTarball { fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash; sha256 = lock.nodes.flake-compat.locked.narHash;
} }
) { ) {
src = ./.; src = ./.;
}).defaultNix })
.defaultNix

109
flake.nix
View File

@ -6,16 +6,25 @@
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
naersk.url = "github:nix-community/naersk"; naersk.url = "github:nix-community/naersk";
flake-compat = { url = github:edolstra/flake-compat; flake = false; }; flake-compat = {
url = github:edolstra/flake-compat;
flake = false;
};
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
}; };
outputs = {nixpkgs, flake-utils, naersk, rust-overlay, ...}: outputs = {
nixpkgs,
flake-utils,
naersk,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system: let
overlays = [ overlays = [
rust-overlay.overlays.default rust-overlay.overlays.default
]; ];
pkgs = import nixpkgs { inherit system overlays; }; pkgs = import nixpkgs {inherit system overlays;};
rust-bin = pkgs.rust-bin.stable."1.71.0".default; rust-bin = pkgs.rust-bin.stable."1.71.0".default;
naersk-lib = pkgs.callPackage naersk { naersk-lib = pkgs.callPackage naersk {
rustc = rust-bin; rustc = rust-bin;
@ -25,70 +34,39 @@
name = "heracles"; name = "heracles";
verion = "0.0.1"; verion = "0.0.1";
src = ./.; src = ./.;
nativeBuildInputs = [ pkgs.pkg-config ]; nativeBuildInputs = [pkgs.pkg-config];
buildInputs = ( buildInputs =
if pkgs.stdenv.isDarwin then (
with pkgs.darwin.apple_sdk.frameworks; [ Security SystemConfiguration ] if pkgs.stdenv.isDarwin
else then with pkgs.darwin.apple_sdk.frameworks; [Security SystemConfiguration]
[ pkgs.openssl ]) ++ [rust-bin]; else [pkgs.openssl]
)
++ [rust-bin];
}; };
in in {
{
packages.default = heracles; packages.default = heracles;
}) // { formatter = pkgs.alejandra;
nixosModule = {config, pkgs, lib}: with lib; { })
// {
nixosModules.default = {
config,
pkgs,
lib,
}: {
options = { options = {
services.heracles.enable = mkEnableOption "enable heracles service"; services.heracles.enable = lib.mkEnableOption "enable heracles service";
services.heracles.listen = mkOption { services.heracles.listen = lib.mkOption {
description = "[host]:port address for heracles to listen on"; description = "[host]:port address for heracles to listen on";
default = "localhost:8080"; default = "localhost:8080";
defaultText = "localhost:8080"; defaultText = "localhost:8080";
type = lib.types.string;
}; };
services.heracles.settings = mkOption { services.heracles.settings = lib.mkOption {
description = "heracles dashboard Configuration"; description = "heracles dashboard Configuration";
default = [ type = lib.types.listOf lib.types.attrs;
{ default = [];
title = "A dashboard"; defaultText = lib.literalExpression ''
graphs = [
{
title = "Graph title";
query_type = "Range";
# yaxis formatting default for this graph
d3_tick_format = "~s";
plots = [
{
source = "http://heimdall:9001";
query = ''
sum by (instance)(irate(node_cpu_seconds_total{job="nodestats"}[5m]))
'';
meta = {
name_label = "instance";
name_prefix = "trace name prefix";
name_suffix = "trace name suffix";
named_axis = "y";
# yaxis formatting for this subplot
d3_tick_format = "~s";
};
}
];
# span for this graph.
span = {
end = "now";
duration = "1d";
step_duration = "10min";
};
}
];
# default span for dashboard
span = {
end = "now";
duration = "1d";
step_duration = "10min";
};
}
];
defaultText = ''
[ [
{ {
title = "A dashboard"; title = "A dashboard";
@ -134,18 +112,21 @@
}; };
}; };
config = mkIf config.services.heracles.enable { config = let
cfg = config.services.heracles;
in
lib.mkIf cfg.enable {
environment.etc."heracles.yaml" = { environment.etc."heracles.yaml" = {
text = (generators.toYAML {} config.services.heracles.settings); text = lib.generators.toYAML {} cfg.settings;
}; };
systemd.services.heracles = { systemd.services.heracles = {
wantedBy = [ "multi-user.target" "default.target" ]; wantedBy = ["multi-user.target" "default.target"];
wants = [ "network.target" ]; wants = ["network.target"];
after = [ "network-online.target" ]; after = ["network-online.target"];
serviceConfig = { serviceConfig = {
Restart = "on-failure"; Restart = "on-failure";
RestartSec = "30s"; RestartSec = "30s";
ExecStart = "${pkgs.heracles}/bin/heracles --listen ${config.services.heracles.listen} --config=${config.environment.etc."heracles.yaml".target}"; ExecStart = "${pkgs.heracles}/bin/heracles --listen ${cfg.listen} --config=${config.environment.etc."heracles.yaml".target}";
}; };
}; };
}; };