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

Add nix flake #3485

Merged
merged 1 commit into from
Mar 19, 2024
Merged

Add nix flake #3485

merged 1 commit into from
Mar 19, 2024

Conversation

brianmay
Copy link
Collaborator

@brianmay brianmay commented Nov 23, 2023

This will add support for nix flakes. Which really helps with my development on nixos. This has two basic parts:

  1. DEVELOPMENT

After this you can use the following commands on computer with nix and a patched version of direnv:

direnv allow
devenv up

And it will automatically start and instance of postgresql and mosquitto for development.

And the environment variables are already configured for teslamate.

So then can start teslamate with:

npm install --prefix ./assets && npm run deploy --prefix ./assets iex -S mix phx.server

This part is all working as designed. And pretty simple and straight forward.

  1. NIX PACKAGE OF TESLAMATE

This is 99% complete, but some pain points I haven't addressed yet. Because I don't yet know of good solutions.

  • There are two checksums in flake.nix that needs to be updated whenever mix.lock or package-lock.json changes.

@brianmay
Copy link
Collaborator Author

Not sure how people feel about nix support. Just adds files, no change to existing functionality.

Can be simplified if required by leaving off the NIX packaging, which isn't currently working anyway.

@JakobLichterfeld
Copy link
Collaborator

Do you run your TeslaMate production on NixOS as well? As docker container or as flake?

@brianmay
Copy link
Collaborator Author

brianmay commented Feb 4, 2024

At present I am currently doing deploys using Docker.

Nix would be better, but haven't got to that point just yet. And would be better if we could avoid the need for the manual update steps in the package build process.

At present time for my other Elixir projects (which I do deploy with Nix) when dependabot updates stuff, I have to manual update things to ensure the nix package build works, which is kind of yuck.

Just setting up a development environment is easy though. This PR currently attempts to do both. Wondering if maybe I should limit it to the development environment as the first step.

Copy link

netlify bot commented Feb 7, 2024

Deploy Preview for teslamate ready!

Name Link
🔨 Latest commit 3384dd8
🔍 Latest deploy log https://app.netlify.com/sites/teslamate/deploys/65f917d966cbc300083da43e
😎 Deploy Preview https://deploy-preview-3485--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.

@brianmay
Copy link
Collaborator Author

brianmay commented Feb 7, 2024

Simplified the node dependencies.

At present the package build is attempting to download locale files at build time, which is a big no-no as far as nix is concerned for reproducible reasons.

@brianmay
Copy link
Collaborator Author

brianmay commented Feb 7, 2024

It builds now. Wow. Amazing.

No idea if it actually works. That can come later.

And the ability to override the LOCALES directory by environment variable probably needs more work. Ideally it should be possible to build it without that environment variable set. Should be easy to fix.

This is why the CI failed.

@brianmay
Copy link
Collaborator Author

brianmay commented Feb 7, 2024

Mostly works.

Currently getting errors that the token is expired and cannot be refreshed. That is not surprising with this old database.

Also getting errors from tzdata cannot write to read only file system. In theory this should be easy, just change the data directory:

config :tzdata, :data_dir, "/writable/directory"

But this did not work last I tried (I can't remember why). You can also disable auto updates entirely:

config :tzdata, :autoupdate, :disabled

Not sure I like this.

I ended up switching to the tz library for my other projects. Which appeared to help, but in this library auto updates are disabled by default, and I forgot to enable them. Regardless, this library seems to be better supported.

Relevant docs:

https://github.com/lau/tzdata
https://github.com/mathieuprog/tz

@brianmay
Copy link
Collaborator Author

Have fixed the tzdata error.

Have mixed feeling here, this will ensure tzdata is the latest version. And I have never had an update break anything.

But maybe if we just disable auto-updates:

config :tzdata, :autoupdate, :disabled

This would make stuff more reproducible. And looking at the problems that our users have trying to set TZ correctly, this might be a good thing.

@brianmay
Copy link
Collaborator Author

Some reason the "Merge GHCR images" is failing. Not exactly sure what it is trying to do...

@JakobLichterfeld
Copy link
Collaborator

Some reason the "Merge GHCR images" is failing. Not exactly sure what it is trying to do...

Sorry about the failed CI, see: #3689

@brianmay
Copy link
Collaborator Author

No problem. When this is fixed in master I will update this pull request.

@JakobLichterfeld
Copy link
Collaborator

No problem. When this is fixed in master I will update this pull request.

Fixed in the master, thanks for your patience

@brianmay
Copy link
Collaborator Author

  • Rebased against latest master.
  • Added entry to CHANGELOG.md.
  • Added teslamate_psql and teslamate_sub wrapper scripts around psql and mosquitto_sub in devenv for convenience.
  • Changed dev postgresql port to 7000 and teslmate port to 7001 so they don't conflict with already running services (if any).

This will add support for nix flakes. Which really helps with my
development on nixos. This has two basic parts:

1. DEVELOPMENT

After this you can use the following commands on computer with nix and a
patched version of direnv:

direnv allow
devenv up

And it will automatically start and instance of postgresql and mosquitto
for development.

And the environment variables are already configured for teslamate.

So then can start teslamate with:

npm install --prefix ./assets && npm run deploy --prefix ./assets
iex -S mix phx.server

This part is all working as designed. And pretty simple and straight
forward.

2. NIX PACKAGE OF TESLA

This is 99% complete, but some pain points I haven't addressed yet.
Because I don't yet know of good solutions.

* There are two checksums in flake.nix that needs to be updated whenever
  mix.lock or package-lock.json changes.
@brianmay
Copy link
Collaborator Author

For reasons I don't understand, trying to use the default value for tzdata's data_dir does not work with the Docker container:

Mar 19 15:44:33 iot2 teslamate[48492]: ** (File.Error) could not write to file "/opt/app/lib/tzdata-1.1.1/priv/latest_remote_poll.txt": permission denied

So I changed the default to /tmp instead.

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.

lgtm, thanks for your efforts!

@JakobLichterfeld
Copy link
Collaborator

Imo we can merge, aren't we?

@brianmay
Copy link
Collaborator Author

Yes, I think I am good to merge now.

@JakobLichterfeld JakobLichterfeld merged commit 2b5cdbd into teslamate-org:master Mar 19, 2024
12 checks passed
@brianmay brianmay deleted the nix_flake branch March 19, 2024 21:14
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.

2 participants