Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amazon-cloudwatch-agent: let users specify configuration file paths #358559

Merged

Conversation

commiterate
Copy link
Contributor

@commiterate commiterate commented Nov 23, 2024

Let users specify configuration file paths.

This is to allow dynamic configuration at runtime (mostly during boot since the systemd unit won't restart unless something runs nixos-rebuild switch/test).

Without this feature, users creating NixOS VM images need to create 1 image per agent configuration permutation if they want immutable images (i.e. don't run nixos-rebuild switch on boot).

This can result in a lot of near-identical images whose only difference is a few options in their CloudWatch Agent configuration (e.g. region option for multi-region AWS deployments). Since VM images are usually on the order of GBs, we end up with a lot of wasted space in Nix stores.

With this feature, users can write their own systemd units that dynamically create configurations (e.g. using information from IMDS, AWS SSM Parameter Store, or AWS Secrets Manager) and write them to a static path.

The tradeoff, however, is that mutable configuration files can cause drift if a change isn't followed by a systemctl restart amazon-cloudwatch-agent.service. nixos-rebuild won't work because we can't access files outside the Nix store in restricted evaluation mode (e.g. can't hash the file and feed it into restartTriggers).

Also doing some small cleanup.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

Maintainer Pings

@philipmw

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 23, 2024
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch 2 times, most recently from 694515b to 648cfce Compare November 24, 2024 00:48
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Nov 24, 2024
@commiterate commiterate marked this pull request as draft November 24, 2024 19:26
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 648cfce to f89ae4a Compare November 24, 2024 20:24
@commiterate commiterate marked this pull request as ready for review November 24, 2024 20:26
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from f89ae4a to 612721a Compare November 24, 2024 20:31
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 612721a to d01112c Compare November 24, 2024 20:59
@commiterate commiterate marked this pull request as draft November 24, 2024 21:05
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from d01112c to 68a28f5 Compare November 24, 2024 21:50
@commiterate commiterate marked this pull request as ready for review November 24, 2024 21:51
@philipmw
Copy link
Contributor

This makes sense to me, but I am concerned about someone specifying both a file and an inline config and having the inline config silently ignored. (I do see you document that the file takes precedence.) Do you think we should error out if both are specified?

@commiterate
Copy link
Contributor Author

commiterate commented Nov 29, 2024

This makes sense to me, but I am concerned about someone specifying both a file and an inline config and having the inline config silently ignored. (I do see you document that the file takes precedence.) Do you think we should error out if both are specified?

I was imagining someone might define configuration as a default in some shareable NixOS config. Consumers can then override by redefining configuration or just using configurationFile. In the latter case, erroring out creates bad UX.

It looks like several other modules which offer both attribute set and path options for configurations (e.g. services.gitlab-runner, virtualisation.containerd) also make the path option take precedence over the attribute set rather than failing builds.

@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 68a28f5 to d14a440 Compare November 29, 2024 02:49
@commiterate
Copy link
Contributor Author

commiterate commented Nov 29, 2024

I guess the other option is to do:

{
  lib,
  pkgs,
  config,
  ...
}:
let
  cfg = config.services.amazon-cloudwatch-agent;

  jsonFormat = pkgs.formats.json { };

  configurationFile =
    if (builtins.typeOf configuration == "path") then
      cfg.configuration
    else
      (jsonFormat.generate "amazon-cloudwatch-agent.json" cfg.configuration);

  # ...
in
{
  options.services.amazon-cloudwatch-agent = {
    configuration = lib.mkOption {
      type = lib.types.oneOf [
        lib.types.path
        jsonFormat.type
      ];
      default = { };
    };
  };
}

Not actually sure if there's another module that does this. Didn't see one from a cursory search.

https://github.com/search?q=repo%3ANixOS%2Fnixpkgs+oneOf+path+json+path%3A%2F%5Enixos%5C%2Fmodules%5C%2Fservices%5C%2F%2F&type=code

This isn't as nice IMO because the example option in the lib.mkOption function would only be one of the values, so the documentation won't have examples of both types.

It also gets messy in the future if the configuration format is in some transitional period. Fluent Bit has this problem since they have a legacy classic .conf format (this is custom) as well as the current YAML format. Having a type like configuration: jsonFormat.type | yamlFormat.type might make it impossible to know which attribute set serializer to use without some type discriminator (might be another field like configurationFormat).

Separating out each type a configuration might be (path, JSON, YAML, TOML) is probably going to be better on a documentation and dispatch level.

The most spartan approach would be to just drop any quality of life module options that serialize attribute sets and only support paths. That way, people need to supply their own derivations that produce a configuration file. This feels a bit heavy handed though and isn't the way most modules are designed.

@philipmw
Copy link
Contributor

Sounds good to me; thanks for the thoughtful reply. I approve, though (for some reason) my approval isn't requested.

@commiterate
Copy link
Contributor Author

commiterate commented Nov 30, 2024

Not sure if OfBorg waits until all checks pass before it requests maintainers for review (RIP aarch64-darwin) or some part of the maintainer metadata isn't quite right.

@commiterate
Copy link
Contributor Author

commiterate commented Dec 13, 2024

Hmm since all checks passed and it still didn't request a review, might be because the input derivation for the package didn't change and NixOS modules don't actually care about the meta attribute (or at least OfBorg doesn't check it).

@arianvp
Copy link
Member

arianvp commented Dec 14, 2024

you can use codeowners instead for modules. it's indeed not supported

@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from d14a440 to 4d688ad Compare December 16, 2024 01:56
@github-actions github-actions bot added the 6.topic: continuous integration Affects continuous integration (CI) in Nixpkgs, including Ofborg and GitHub Actions label Dec 16, 2024
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch 2 times, most recently from 9cb1a7e to 72a7ea6 Compare December 16, 2024 04:52
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch 3 times, most recently from 046b30a to 36278a3 Compare December 17, 2024 17:46
@github-actions github-actions bot added 10.rebuild-darwin: 1-10 and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin labels Dec 17, 2024
@ofborg ofborg bot requested a review from philipmw December 18, 2024 05:05
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 36278a3 to 6c17dd3 Compare December 18, 2024 18:18
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 6c17dd3 to 1a38fd2 Compare December 19, 2024 18:50
@commiterate
Copy link
Contributor Author

Apparently fetchFromGitHub now accepts a tag attribute. Going to swap to that.

@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 1a38fd2 to fa1ed8e Compare December 22, 2024 18:33
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from fa1ed8e to 149dc03 Compare December 22, 2024 19:31
@commiterate commiterate force-pushed the update/amazon-cloudwatch-agent branch from 149dc03 to 43caf2e Compare December 23, 2024 03:58
@ofborg ofborg bot requested a review from philipmw December 23, 2024 17:31
@philiptaron philiptaron merged commit 297e5bb into NixOS:master Dec 23, 2024
43 of 45 checks passed
@commiterate commiterate deleted the update/amazon-cloudwatch-agent branch December 23, 2024 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: continuous integration Affects continuous integration (CI) in Nixpkgs, including Ofborg and GitHub Actions 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 10.rebuild-darwin: 1 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants