summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/fetchitchio/default.nix
blob: 0b27914eb8e43e06f69dc3bfd393dadf5f168449 (plain)
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
{
  lib,
  stdenvNoCC,
  cacert,
  python3,
}:

lib.extendMkDerivation {
  constructDrv = stdenvNoCC.mkDerivation;
  excludeDrvArgNames = [
    "derivationArgs"
    "sha1"
    "sha256"
    "sha512"
  ];
  extendDrvArgs =
    finalAttrs:
    lib.fetchers.withNormalizedHash { } (
      {

        endpoint ? "https://api.itch.io",

        # The name of the environment variable that contains the itch.io API key.
        # The environment variable needs to be set for the nix building process,
        # which is nix-daemon for multi-user mode.
        apiKeyVar ? "NIX_ITCHIO_API_KEY",

        # The game store page URL in the format of https://{author}.itch.io/{game}
        gameUrl,

        # The upload ID of the downloadable file.
        # To get the upload ID, look at the request URL when you download it.
        upload,

        # Derivation name.
        name ? null,

        # The extra message printed when the API key is not provided
        # or when the account of the API key did not purchase the game.
        extraMessage ? null,

        # Show the download URL without actually downloading it, for testing purposes.
        # Notice that this can potentially leak the API key.
        showUrl ? false,

        outputHash ? lib.fakeHash,
        outputHashAlgo ? null,
        preFetch ? "",
        postFetch ? "",
        nativeBuildInputs ? [ ],
        impureEnvVars ? [ ],
        passthru ? { },
        meta ? { },
        preferLocalBuild ? true,
        derivationArgs ? { },
      }:
      let
        finalHashHasColon = lib.hasInfix ":" finalAttrs.hash;
        finalHashColonMatch = lib.match "([^:]+)[:](.*)" finalAttrs.hash;
      in
      derivationArgs
      // {
        __structuredAttrs = true;

        name = if name != null then name else baseNameOf gameUrl;

        hash =
          if outputHashAlgo == null || outputHash == "" || lib.hasPrefix outputHashAlgo outputHash then
            outputHash
          else
            "${outputHashAlgo}:${outputHash}";
        outputHash =
          if finalAttrs.hash == "" then
            lib.fakeHash
          else if finalHashHasColon then
            lib.elemAt finalHashColonMatch 1
          else
            finalAttrs.hash;
        outputHashAlgo = if finalHashHasColon then lib.head finalHashColonMatch else null;
        outputHashMode = "flat";

        nativeBuildInputs = [
          cacert
          python3
        ]
        ++ nativeBuildInputs;

        strictDeps = true;

        inherit preferLocalBuild;

        # ENV
        nixpkgsVersion = lib.trivial.release;
        uploadName = name;
        inherit
          endpoint
          apiKeyVar
          gameUrl
          extraMessage
          showUrl
          preFetch
          postFetch
          ;
        impureEnvVars =
          lib.fetchers.proxyImpureEnvVars
          ++ [
            apiKeyVar
            "NIX_CONNECT_TIMEOUT"
          ]
          ++ impureEnvVars;

        builder = builtins.toFile "builder.sh" ''
          source "$NIX_ATTRS_SH_FILE"
          runHook preFetch
          python ${./fetchitchio.py}
          runHook postFetch
        '';
      }
    );

  inheritFunctionArgs = false;
}