Skip to content

nixos/esphome: Module Not Found and Permissions Issues #504950

Closed

Description

@BeatLink

Nixpkgs version

  • Unstable (26.05)

Describe the bug

When services.esphome is enabled and configured, the dashboard commands fail with no such module found.

I managed to solve it by adding

PYTHONPATH = "${pkgs.esphome}/lib/python3.13/site-packages:${pkgs.python3Packages.makePythonPath pkgs.esphome.dependencies}";

to the service's environment

Once that is solved however, the current systemd service with its hardening settings creates several permissions issues when building yaml files. Here is the overrides I'm using which creates a dedicated user and creates an option to specify a custom state directory. Perhaps it can be merged upstream

{
    config,
    lib,
    pkgs,
    ...
}:

let
    inherit (lib)
        maintainers
        mkIf
        mkOption
        types
        ;

    cfg = config.services.esphome;

    esphomeParams =
        if cfg.enableUnixSocket then
            "--socket /run/esphome/esphome.sock"
        else
            "--address ${cfg.address} --port ${toString cfg.port}";
in
{
    meta.maintainers = with maintainers; [ oddlama ];

    options.services.esphome = {
        user = mkOption {
            type = types.str;
            default = "esphome";
            description = "User account under which esphome runs.";
        };

        group = mkOption {
            type = types.str;
            default = "esphome";
            description = "Group under which esphome runs.";
        };

        stateDir = mkOption {
            type = types.path;
            default = "/var/lib/esphome";
            description = "the folder used to store all esphome data.";
        };

    };

    config = mkIf cfg.enable {

        users = {
            users = mkIf (cfg.user == "esphome") {
                esphome = {
                    inherit (cfg) group;
                    isSystemUser = true;
                };
            };
            groups = mkIf (cfg.group == "esphome") { esphome = { }; };
        };

        systemd.tmpfiles.rules = [
            "d ${cfg.stateDir} 0750 ${cfg.user} ${cfg.group} -"
        ];

        systemd.services.esphome = {
            environment = {
                # platformio fails to determine the home directory when using DynamicUser
                PLATFORMIO_CORE_DIR = lib.mkForce "${cfg.stateDir}/.platformio";
                PYTHONPATH = "${pkgs.esphome}/lib/python3.13/site-packages:${pkgs.python3Packages.makePythonPath pkgs.esphome.dependencies}";
            };
            serviceConfig = {
                ExecStart = lib.mkForce "${cfg.package}/bin/esphome dashboard ${esphomeParams} ${cfg.stateDir}";
                User = cfg.user;
                Group = cfg.group;
                DynamicUser = lib.mkForce false;
                WorkingDirectory = lib.mkForce cfg.stateDir;
                StateDirectory = lib.mkForce "";
                StateDirectoryMode = lib.mkForce "";
                ReadWritePaths = [ cfg.stateDir ];
            };
        };
    };
}

Steps to reproduce

add services.esphome.enabled = true to config

    services.esphome = {
        enable = true;
        port = 6052;
};

Go to the web dashboard at https://localhost:6052.

Try to add a device, clean files or perform any other action

Note the error.

Expected behaviour

Commands should work instead of returning module not found: esphome

Screenshots

No response

Relevant log output

Additional context

I managed to solve it by adding

        systemd.services.esphome = {
            environment = {
                PYTHONPATH = "${pkgs.esphome}/lib/python3.13/site-packages:${pkgs.python3Packages.makePythonPath pkgs.esphome.dependencies}";

            };
        };

System metadata

  • system: "x86_64-linux"
  • host os: Linux 6.18.19, NixOS, 26.05 (Yarara), 26.05.20260324.46db2e0
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.31.3
  • nixpkgs: /nix/store/fvb3fhm8c193i6692v5223bxpa5w2nv7-source

Notify maintainers

@oddlama


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a 👍 reaction to issues you find important.

Activity

  1. added
    0.kind: bug
    6.topic: nixos
    on Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug6.topic: nixos

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions