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

feat: Create NixOS module #3998

Merged
merged 2 commits into from
Jul 2, 2024

Conversation

scottbot95
Copy link
Contributor

Overview

Adds a semi-opinionated NixOS module to ease deploying TeslaMate on NixOS.

Trade-offs

  • Does not use docker/podman for service. This was done as it is typical for NixOS services to run natively since Nix itself + systemd provides much the same isolation/reproducibility that docker does.
  • Uses built-in NixOS grafana/postgres modules. This makes development easier/more-consistent however it does mean we are polluting the "global" grafana/postgres instances. I felt this was worth the trade-off but am open to discussion.
  • Provides native nix bindings for most teslamate configuration but not all. I chose the minimal set of configurations option that would be required to run Teslamate+Grafana behind a reverse proxy. Additionally, any missing configuration can still be provided by adding to systemd.services.teslamate.environment
  • Does not setup MQTT server. I chose not to include this since setting up a MQTT server in NixOS is quite simple and TeslaMate is not opinionated on its configuration.
  • RELEASE_COOKIE must be passed in via the secretFile. Values used in the NixOS config are globally readable so secrets should be read from disk at runtime. We could use either a hard-coded RELEASE_COOKIE value or generate something based on hashes, but given it is technically a secret, I chose to just require the end user to provide it.

Testing

nix flake check --impure (--impure require due to devenv) will run a simple telsamate configuration in a VM and assert that a server starts up on port 4000

Copy link

netlify bot commented Jun 25, 2024

Deploy Preview for teslamate ready!

Name Link
🔨 Latest commit 6426933
🔍 Latest deploy log https://app.netlify.com/sites/teslamate/deploys/667cce906dcd210008520f3c
😎 Deploy Preview https://deploy-preview-3998--teslamate.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@brianmay brianmay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, looks good to me.

flake.nix Outdated
imports = [ self.nixosModules.default ];
services.teslamate = {
enable = true;
secrestFile = builtins.toFile "teslamate.env" ''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible typo in secretsFile here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yeah. Those force pushes earlier were fixing that, I guess I missed this one lol

module.nix Show resolved Hide resolved
@JakobLichterfeld JakobLichterfeld added the enhancement New feature or request label Jun 26, 2024
@JakobLichterfeld
Copy link
Collaborator

I love the work on the NixOS side. What I wonder is how to do backup and restore with this implementation? This is the only reason why I have not yet migrated my TeslaMate instance to my NixOS server.

@brianmay
Copy link
Collaborator

Backup/restore is just:

  • Backup the database as usual.
  • Backup the nixos configuration for the system that lets you recreate the system. Personally I have a nix flake in a central git that lets me configure all my systems.

I have not used postgresql on nixos, so I am not really familiar with upgrading databases, etc. I think I saw a webpage with instructions somewhere.

The potential gotcha is configuring secrets. You don't want to include secrets in plain text format in the nix repo, or at least this is frowned upon. I personally use agenix for these.

@brianmay
Copy link
Collaborator

At present I just install teslamate on my nixos system with a docker image, but I think using this flake will be simpler.

@JakobLichterfeld
Copy link
Collaborator

JakobLichterfeld commented Jun 26, 2024

  • Backup the database as usual.

Sometimes the most obvious is the hardest to see :-)

  • Backup the nixos configuration for the system that lets you recreate the system. Personally I have a nix flake in a central git that lets me configure all my systems.

Yeah, that's what I do as well, https://github.com/JakobLichterfeld/nix-config

The potential gotcha is configuring secrets. You don't want to include secrets in plain text format in the nix repo, or at least this is frowned upon. I personally use agenix for these.

Same for me, using agenix for secrets and git-crypt as well

Copy link
Collaborator

@JakobLichterfeld JakobLichterfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY!

@JakobLichterfeld JakobLichterfeld merged commit 25c1611 into teslamate-org:master Jul 2, 2024
12 checks passed
@scottbot95 scottbot95 deleted the nix-module branch July 3, 2024 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants