├── .gitignore ├── .editorconfig ├── default.nix ├── lib ├── json-files.nix └── helpers.nix └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /.history 2 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 2 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | { lib, pkgs, ... }: 2 | 3 | let 4 | api-root = pkgs.callPackage ./lib/json-files.nix { 5 | api-paths = { 6 | hello = { 7 | status = 200; 8 | response = "Hello!"; 9 | }; 10 | }; 11 | }; 12 | in { 13 | services.nginx = { 14 | enable = true; 15 | 16 | virtualHosts."api.local" = { 17 | onlySSL = false; 18 | locations."/".root = "${api-root}"; 19 | }; 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /lib/json-files.nix: -------------------------------------------------------------------------------- 1 | { stdenv, lib, api-paths, ... }: 2 | 3 | let 4 | helpers = import ./helpers.nix { lib = lib; }; 5 | apiFiles = helpers.conervetApiPathsToTextFiles { apiPaths = api-paths; }; 6 | apiFilesCpLines = helpers.getCpLinesForApiPathsFiles { apiFiles = apiFiles; }; 7 | 8 | in stdenv.mkDerivation { 9 | name = "NixOS-based API"; 10 | dontUnpack = true; 11 | 12 | installPhase = '' 13 | mkdir -p "$out"; 14 | ${apiFilesCpLines} 15 | ''; 16 | } 17 | -------------------------------------------------------------------------------- /lib/helpers.nix: -------------------------------------------------------------------------------- 1 | { lib }: { 2 | conervetApiPathsToTextFiles = { apiPaths }: 3 | lib.mapAttrs (k: v: 4 | let jsonText = builtins.toJSON v; 5 | in rec { 6 | path = builtins.toFile "${k}.json" "${jsonText}"; 7 | copyCmd = ''cp '${path}' "$out/${k}.json"''; 8 | }) apiPaths; 9 | 10 | getCpLinesForApiPathsFiles = { apiFiles }: 11 | lib.foldl' (acc: apiPathName: 12 | (let copyCmd = apiFiles."${apiPathName}".copyCmd; 13 | in '' 14 | ${acc} 15 | ${copyCmd} 16 | '')) "" (lib.attrNames apiFiles); 17 | } 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 jim3692 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------