Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@PatMulligan
Copy link
Contributor

Fix LNBits NixOS Service Module for Python Virtual Environment Package

Problem

The current LNBits NixOS service module (nix/modules/lnbits-service.nix) has several issues that prevent it from working with the flake-built Python virtual environment package:

  1. Missing src attribute: The module expects cfg.package.src but the flake builds a Python virtual environment package that doesn't have a src attribute
  2. Incorrect binary path: lib.getExe resolves to the wrong binary name (lnbits-env instead of lnbits)
  3. Wrong working directory: The service needs to run from where the Python package's static files and templates are located

Solution

This PR fixes the service module to work correctly with the virtual environment package structure produced by the flake.

Changes Made

1. Removed dependency on non-existent src attribute

- LNBITS_PATH = "${cfg.package.src}";

The LNBITS_PATH environment variable was removed as it references a non-existent src attribute.

2. Fixed working directory to point to installed Python package

- WorkingDirectory = "${cfg.package.src}";
+ WorkingDirectory = "${cfg.package}/lib/python3.12/site-packages";

LNBits needs to run from the directory containing its static files and templates. In the virtual environment package, these are located in the site-packages directory.

3. Fixed binary path resolution

- ExecStart = "${lib.getExe cfg.package} --port ${toString cfg.port} --host ${cfg.host}";
+ ExecStart = "${cfg.package}/bin/lnbits --port ${toString cfg.port} --host ${cfg.host}";

The lib.getExe function was incorrectly resolving to lnbits-env (the package name) instead of the actual lnbits binary. Using the explicit path ensures the correct binary is executed.

4. Fixed StateDirectory to use relative path

- StateDirectory = "${cfg.stateDir}";
+ StateDirectory = "lnbits";

Changed to use the relative path "lnbits" which systemd will automatically create under /var/lib/.

5. Proper data folder structure

+ systemd.tmpfiles.rules = [
+   "d ${cfg.stateDir}                            0700 ${cfg.user} ${cfg.group} - -"
+   "d ${cfg.stateDir}/data                       0700 ${cfg.user} ${cfg.group} - -"
+ ];

  environment = lib.mkMerge [
    {
-     LNBITS_DATA_FOLDER = "${cfg.stateDir}";
-     LNBITS_EXTENSIONS_PATH = "${cfg.stateDir}/extensions";
-     LNBITS_PATH = "${cfg.package.src}";
+     LNBITS_DATA_FOLDER = "${cfg.stateDir}/data";
+     # LNBits automatically appends '/extensions' to this path
+     LNBITS_EXTENSIONS_PATH = "${cfg.stateDir}";
    }

Fixed the folder structure to properly separate data and extensions:

  • Data files (database.sqlite3, images/, logs/, upgrades/) now go in ${stateDir}/data
  • Extensions go in ${stateDir}/extensions (LNBits automatically appends /extensions to the path)
  • Added tmpfiles rules to ensure directories are created with proper permissions
  • Note: LNBITS_EXTENSIONS_PATH is set to ${stateDir} because LNBits internally appends /extensions to this path

Testing

The fixed service module has been tested in a NixOS VM with the following results:

✅ Service starts successfully
✅ Web interface accessible at configured port
✅ Database migrations run correctly
✅ Static files and templates load properly

Test Configuration Used

services.lnbits = {
  enable = true;
  host = "0.0.0.0";
  port = 5000;
  openFirewall = true;
  env = {
    LNBITS_ADMIN_UI = "true";
  };
};

Verification Commands

# Service status
systemctl status lnbits
# ● lnbits.service - lnbits
#      Active: active (running)

# Web interface test
curl -I http://localhost:5000/
# HTTP/1.1 307 Temporary Redirect
# location: /first_install

# Process verification
ps aux | grep lnbits
# python3.12 /nix/store/.../bin/lnbits --port 5000 --host 0.0.0.0

# Verify folder structure
ls -la /var/lib/lnbits/
# drwx------ 5 lnbits lnbits 4096 data
# drwxr-xr-x 2 lnbits lnbits 4096 extensions

ls -la /var/lib/lnbits/data/
# -rw-r--r-- 1 lnbits lnbits 143360 database.sqlite3
# drwxr-xr-x 2 lnbits lnbits   4096 images
# drwxr-xr-x 2 lnbits lnbits   4096 logs
# drwxr-xr-x 2 lnbits lnbits   4096 upgrades

Impact

This fix enables users to:

  • Successfully deploy LNBits on NixOS using the flake
  • Use the NixOS service module for production deployments
  • Configure LNBits declaratively through NixOS configuration

Backwards Compatibility

These changes maintain backwards compatibility with existing configurations. The service interface remains unchanged - only the internal implementation is fixed to work with the virtual environment package structure.

Related Issues

This fixes the error encountered when using the flake's NixOS module:

error: attribute 'src' missing
at /etc/nixos/sources/lnbits/nix/modules/lnbits-service.nix:111:31

Additional Notes

The Python version (3.12) is currently hardcoded in the working directory path. A future improvement could make this dynamic based on the Python version used by the package.

@PatMulligan PatMulligan force-pushed the fix-nixos-module branch 3 times, most recently from 460d310 to 74917db Compare September 16, 2025 16:14
@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.19%. Comparing base (c05122e) to head (56bd61a).
⚠️ Report is 2 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #3363      +/-   ##
==========================================
+ Coverage   57.17%   57.19%   +0.01%     
==========================================
  Files         113      113              
  Lines       14451    14451              
==========================================
+ Hits         8263     8265       +2     
+ Misses       6188     6186       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

stateDir folder is better organized into data and extensions
@arcbtc
Copy link
Member

arcbtc commented Sep 17, 2025

Tested and works. I used these install guide tweaks, would they need to be different?
https://github.com/lnbits/lnbits/pull/3361/files

@arcbtc arcbtc requested review from motorina0 and prusnak September 17, 2025 21:20
Copy link
Collaborator

@motorina0 motorina0 left a comment

Choose a reason for hiding this comment

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

LGTM: did not tested

@dni dni merged commit 210b8a7 into lnbits:dev Sep 26, 2025
45 checks passed
arcbtc added a commit to arcbtc/lnbits that referenced this pull request Oct 8, 2025
arcbtc added a commit to arcbtc/lnbits that referenced this pull request Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants