at 25.11-pre 2.2 kB view raw
1{ 2 config, 3 lib, 4 options, 5 pkgs, 6 ... 7}: 8let 9 10 name = "headphones"; 11 12 cfg = config.services.headphones; 13 opt = options.services.headphones; 14 15in 16 17{ 18 19 ###### interface 20 21 options = { 22 services.headphones = { 23 enable = lib.mkOption { 24 type = lib.types.bool; 25 default = false; 26 description = "Whether to enable the headphones server."; 27 }; 28 dataDir = lib.mkOption { 29 type = lib.types.path; 30 default = "/var/lib/${name}"; 31 description = "Path where to store data files."; 32 }; 33 configFile = lib.mkOption { 34 type = lib.types.path; 35 default = "${cfg.dataDir}/config.ini"; 36 defaultText = lib.literalExpression ''"''${config.${opt.dataDir}}/config.ini"''; 37 description = "Path to config file."; 38 }; 39 host = lib.mkOption { 40 type = lib.types.str; 41 default = "localhost"; 42 description = "Host to listen on."; 43 }; 44 port = lib.mkOption { 45 type = lib.types.ints.u16; 46 default = 8181; 47 description = "Port to bind to."; 48 }; 49 user = lib.mkOption { 50 type = lib.types.str; 51 default = name; 52 description = "User to run the service as"; 53 }; 54 group = lib.mkOption { 55 type = lib.types.str; 56 default = name; 57 description = "Group to run the service as"; 58 }; 59 }; 60 }; 61 62 ###### implementation 63 64 config = lib.mkIf cfg.enable { 65 66 users.users = lib.optionalAttrs (cfg.user == name) { 67 ${name} = { 68 uid = config.ids.uids.headphones; 69 group = cfg.group; 70 description = "headphones user"; 71 home = cfg.dataDir; 72 createHome = true; 73 }; 74 }; 75 76 users.groups = lib.optionalAttrs (cfg.group == name) { 77 ${name}.gid = config.ids.gids.headphones; 78 }; 79 80 systemd.services.headphones = { 81 description = "Headphones Server"; 82 wantedBy = [ "multi-user.target" ]; 83 after = [ "network.target" ]; 84 serviceConfig = { 85 User = cfg.user; 86 Group = cfg.group; 87 ExecStart = "${pkgs.headphones}/bin/headphones --datadir ${cfg.dataDir} --config ${cfg.configFile} --host ${cfg.host} --port ${toString cfg.port}"; 88 }; 89 }; 90 }; 91}