all repos — mycal @ main

private calendar anonymiser

flake.nix (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

    flake-utils.url = "github:numtide/flake-utils";

    pyproject-nix = {
      url = "github:pyproject-nix/pyproject.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    uv2nix = {
      url = "github:pyproject-nix/uv2nix";
      inputs.pyproject-nix.follows = "pyproject-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    pyproject-build-systems = {
      url = "github:pyproject-nix/build-system-pkgs";
      inputs.pyproject-nix.follows = "pyproject-nix";
      inputs.uv2nix.follows = "uv2nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    { self
    , nixpkgs
    , flake-utils
    , uv2nix
    , pyproject-nix
    , pyproject-build-systems
    , ...
    }: flake-utils.lib.eachDefaultSystem
      (system:
      let
        inherit (nixpkgs) lib;

        # Load a uv workspace from a workspace root.
        # Uv2nix treats all uv projects as workspace projects.
        workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };

        # Create package overlay from workspace.
        overlay = workspace.mkPyprojectOverlay {
          # Prefer prebuilt binary wheels as a package source.
          # Sdists are less likely to "just work" because of the metadata missing from uv.lock.
          # Binary wheels are more likely to, but may still require overrides for library dependencies.
          sourcePreference = "wheel"; # or sourcePreference = "sdist";
          # Optionally customise PEP 508 environment
          # environ = {
          #   platform_release = "5.10.65";
          # };
        };

        pkgs = import nixpkgs { inherit system; };
        python = pkgs.python3;

        # Extend generated overlay with build fixups
        #
        # Uv2nix can only work with what it has, and uv.lock is missing essential metadata to perform some builds.
        # This is an additional overlay implementing build fixups.
        # See:
        # - https://pyproject-nix.github.io/uv2nix/FAQ.html
        pyprojectOverrides = final: prev: {
          toml-dataclass = prev.toml-dataclass.overrideAttrs (old: {
            nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ final.resolveBuildSystem {
              setuptools = [ ];
            };
          });
        };

        # Construct package set
        pythonSet =
          # Use base package set from pyproject.nix builders
          (pkgs.callPackage pyproject-nix.build.packages {
            inherit python;
          }).overrideScope
            (
              lib.composeManyExtensions [
                pyproject-build-systems.overlays.default
                overlay
                pyprojectOverrides
              ]
            );
      in
      {
        packages = {
          default = pythonSet.mkVirtualEnv "mycal" workspace.deps.default;
          inherit python pythonSet;
        };

        devShells = {
          default = pkgs.mkShell {
            packages = (with pkgs; [
              uv
            ]) ++ [
              python
            ];
            inputsFrom = [ self.packages.${pkgs.system}.default ];
            env =
              {
                # Prevent uv from managing Python downloads
                UV_PYTHON_DOWNLOADS = "never";
                # Force uv to use nixpkgs Python interpreter
                UV_PYTHON = python.interpreter;
              }
              // lib.optionalAttrs pkgs.stdenv.isLinux {
                # Python libraries often load native shared objects using dlopen(3).
                # Setting LD_LIBRARY_PATH makes the dynamic library loader aware of libraries without using RPATH for lookup.
                # LD_LIBRARY_PATH = lib.makeLibraryPath pkgs.pythonManylinuxPackages.manylinux1;
              };
            shellHook = ''
              unset PYTHONPATH
            '';
          };
        };
      }) // {
      nixosModules = {
        mycal =
          { config, lib, pkgs, ... }:
          let
            cfg = config.services.mycal;
            settingsFormat = pkgs.formats.toml { };

            inherit (lib) mkEnableOption mkOption mkIf types;
          in
          {
            options.services.mycal = {
              enable = mkEnableOption "Mycal calendar anonymiser";

              port = mkOption {
                type = types.port;
                default = 8000;
                description = ''
                  Port to listen on.
                '';
              };

              host = mkOption {
                type = types.str;
                default = "127.0.0.1";
                description = ''
                  Host to bind to.
                '';
              };

              venv = mkOption {
                type = lib.types.package;
                default = self.packages.${pkgs.system}.default;
                description = ''
                  Mycal virtual environment package
                '';
              };

              settings = mkOption {
                default = { };
                description = ''
                  Additional settings for Mycal.
                '';
                type = types.submodule {
                  freeformType = settingsFormat.type;

                  options = {
                    name = mkOption {
                      type = types.str;
                      description = ''
                        Name of person for whom this calendar is created.
                      '';
                    };

                    timezone = mkOption {
                      type = types.str;
                      default = "Europe/Berlin";
                      description = ''
                        Default timezone for the calendar.
                      '';
                    };

                    update_interval = mkOption {
                      type = types.int;
                      default = 3600;
                      description = ''
                        Interval in seconds between calendar cache updates.
                      '';
                    };

                    calendars = mkOption {
                      default = [ ];
                      description = ''
                        Configuration for multiple calendars.

                        See https://git.alin.ovh/mycal/blob/main/config.toml.example
                      '';

                      type = types.listOf (types.submodule {
                        freeformType = settingsFormat.type;
                      });
                    };

                    email = mkOption {
                      type = types.str;
                      example = "john.doe@example.com";
                      description = ''
                        Email address of the user.
                      '';
                    };
                  };
                };
              };
            };

            config = mkIf cfg.enable {
              systemd.services.mycal = {
                description = "Mycal Calendar Service";

                environment = {
                  CONFIG_FILE = settingsFormat.generate "mycal-config.toml" cfg.settings;
                  TZPATH = "${pkgs.tzdata}/share/zoneinfo";
                };

                serviceConfig = {
                  ExecStart = ''
                    ${cfg.venv}/bin/uvicorn \
                      --host ${cfg.host} \
                      --port ${toString cfg.port} \
                      --ws none \
                      mycal:app
                  '';
                  Restart = "on-failure";

                  DynamicUser = true;
                  StateDirectory = "mycal";
                  RuntimeDirectory = "mycal";

                  BindReadOnlyPaths = [
                    "${config.environment.etc."ssl/certs/ca-certificates.crt".source}:/etc/ssl/certs/ca-certificates.crt"
                    builtins.storeDir
                    "-/etc/resolv.conf"
                    "-/etc/nsswitch.conf"
                    "-/etc/hosts"
                    "-/etc/localtime"
                  ];
                };

                wantedBy = [ "multi-user.target" ];
              };
            };
          };
      };
    };
}