spacepaste

  1.  
  2. { config, lib, pkgs, ... }:
  3. with lib;
  4. let
  5. sheepdog = pkgs.sheepdog;
  6. cfg = config.services.sheepdog;
  7. stateDir = "/var/lib/sheepdog";
  8. pidFile = "${stateDir}/run/sheep.pid";
  9. in
  10. {
  11. ###### interface
  12. options = {
  13. services.sheepdog = {
  14. enable = mkEnableOption "Sheepdog Daemon";
  15. config = mkOption {
  16. type = types.lines;
  17. default = "--cluster local --log dst=syslog --upgrade";
  18. description = ''
  19. Configuration directives that should be added to
  20. <literal>sheep commandline</literal>.
  21. '';
  22. };
  23. pidFile = mkOption {
  24. type = types.string;
  25. default = "${pidFile}";
  26. description = ''
  27. Where to write the pidfile
  28. '';
  29. };
  30. stateDir = mkOption {
  31. type = types.string;
  32. default = "${stateDir}";
  33. description = ''
  34. Where to store the cluster metadata and objects
  35. '';
  36. };
  37. };
  38. };
  39. ###### implementation
  40. config = mkIf config.services.sheepdog.enable {
  41. users.extraUsers = singleton {
  42. name = "sheepdog";
  43. uid = config.ids.uids.sheepdog;
  44. description = "Sheepdog daemon user";
  45. };
  46. users.extraGroups = singleton {
  47. name = "sheepdog";
  48. gid = config.ids.gids.sheepdog;
  49. };
  50. systemd.services.sheepdog = {
  51. description = "Sheepdog daemon";
  52. after = [ "network.target" "systemd-resolved.service" ];
  53. wants = [ "syslog.target" ];
  54. wantedBy = [ "multi-user.target" ];
  55. path = [ sheepdog ];
  56. preStart = ''
  57. if [ ! -d ${cfg.stateDir} ]; then
  58. mkdir -m 755 -p ${cfg.stateDir}
  59. chown -R sheepdog:sheepdog ${cfg.stateDir}
  60. fi
  61. pidFileDir=$(dirname ${cfg.pidFile})
  62. if [ ! -d ''${pidFileDir} ]; then
  63. mkdir -m 755 -p ''${pidFileDir}
  64. chown -R sheepdog:sheepdog ''${pidFileDir}
  65. fi
  66. '';
  67. serviceConfig = {
  68. Type = "forking";
  69. ExecStart = "${sheepdog}/bin/sheep --pidfile ${cfg.pidFile} ${cfg.config} ${cfg.stateDir}";
  70. PIDFile = "${stateDir}/run/sheep.pid";
  71. PermissionsStartOnly = true;
  72. User = "sheepdog";
  73. Group = "sheepdog";
  74. };
  75. };
  76. };
  77. }
  78.