|
| 1 | +# Make sure to run "nix flake update" at least once to generate your flake.lock. |
| 2 | +# Run your main function from Nix by importing this flake as follows: |
| 3 | +# |
| 4 | +# let |
| 5 | +# yourProject = builtins.getFlake "URL"; # for example |
| 6 | +# mainResult = (yourProject.lib.loadGlistixPackage {}).main {}; |
| 7 | +# in doSomethingWith mainResult; |
| 8 | +# |
| 9 | +# See below to customize your flake. |
| 10 | +{ |
| 11 | + description = "json - Work with JSON in Glistix - Port of gleam_json to Nix"; |
| 12 | + |
| 13 | + inputs = { |
| 14 | + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; |
| 15 | + systems.url = "github:nix-systems/default"; |
| 16 | + |
| 17 | + # Used for default.nix and shell.nix. |
| 18 | + flake-compat = { |
| 19 | + url = "github:edolstra/flake-compat"; |
| 20 | + flake = false; |
| 21 | + }; |
| 22 | + |
| 23 | + # Pick your Glistix version here. |
| 24 | + glistix.url = "github:glistix/glistix/v0.1.0"; |
| 25 | + |
| 26 | + # Submodules |
| 27 | + # Add any submodules which you use as dependencies here, |
| 28 | + # and then add them to "submodules = [ ... ]" below. |
| 29 | + stdlib = { |
| 30 | + url = "github:glistix/stdlib"; |
| 31 | + flake = false; |
| 32 | + }; |
| 33 | + }; |
| 34 | + |
| 35 | + outputs = |
| 36 | + inputs@{ self, nixpkgs, flake-parts, systems, glistix, stdlib, ... }: |
| 37 | + let |
| 38 | + # --- CUSTOMIZATION PARAMETERS --- |
| 39 | + |
| 40 | + # Add any source files to keep when building your Glistix package here. |
| 41 | + # This includes anything which might be necessary at build time. |
| 42 | + sourceFiles = [ |
| 43 | + "gleam.toml" |
| 44 | + "manifest.toml" # make sure to build locally at least once so Glistix generates this file |
| 45 | + "src" |
| 46 | + "test" |
| 47 | + "external" # cloned package repositories |
| 48 | + "output" # checked-in build output, if it exists |
| 49 | + "priv" # assets and other relevant files |
| 50 | + ".gitignore" |
| 51 | + ".gitmodules" |
| 52 | + ]; |
| 53 | + |
| 54 | + # Submodules have to be specified here, even if they are cloned locally. |
| 55 | + # Add them as inputs to the flake, and then specify where to clone them to |
| 56 | + # during build below. |
| 57 | + submodules = [ |
| 58 | + { |
| 59 | + src = stdlib; |
| 60 | + dest = "external/stdlib"; |
| 61 | + } |
| 62 | + ]; |
| 63 | + |
| 64 | + # If you cache your build output, this will specify the path |
| 65 | + # 'loadGlistixPackage' will check to find compiled Nix files in by default. |
| 66 | + # You usually don't have to change this, unless you cache your output in a |
| 67 | + # folder other than ./output, or if your output is fetched through a derivation |
| 68 | + # (e.g. `builtins.fetchGit`). |
| 69 | + # Don't forget to add the path to "sourceFiles" above if it's in the repo! |
| 70 | + outputPath = src + "/output"; |
| 71 | + |
| 72 | + # Set this to 'true' if you created an 'output' folder where you're storing |
| 73 | + # build outputs and you'd like to ensure Nix consumers will use it. |
| 74 | + # It will be used even if this is 'false', but Glistix will fallback to |
| 75 | + # building your package from scratch upon load if the output folder is |
| 76 | + # missing. If you set this to 'true', it will error instead. |
| 77 | + forceLoadFromOutput = false; |
| 78 | + |
| 79 | + # --- IMPLEMENTATION --- |
| 80 | + |
| 81 | + inherit (nixpkgs) lib; |
| 82 | + glistixLib = glistix.lib; |
| 83 | + sourceFileRegex = builtins.concatStringsSep "|" (map lib.escapeRegex sourceFiles); |
| 84 | + |
| 85 | + # Filter source files to only include the given files and folders. |
| 86 | + src = lib.sourceByRegex ./. [ "(${sourceFileRegex})(/.*)?" ]; |
| 87 | + |
| 88 | + # Prepare call to 'buildGlistixPackage', allowing for overrides. |
| 89 | + buildGlistixPackage = |
| 90 | + args@{ system, ... }: |
| 91 | + let |
| 92 | + inherit (glistix.builders.${system}) buildGlistixPackage; |
| 93 | + overrides = builtins.removeAttrs args [ "system" ]; |
| 94 | + builderArgs = { |
| 95 | + inherit src submodules; |
| 96 | + } // overrides; |
| 97 | + in |
| 98 | + buildGlistixPackage builderArgs; |
| 99 | + |
| 100 | + # Prepare the call to 'loadGlistixPackage'. This is used to |
| 101 | + # easily run your Gleam code compiled to Nix from within Nix. |
| 102 | + # |
| 103 | + # This will try to read compiled code at 'output/dev/nix', |
| 104 | + # thus not invoking the Glistix compiler at all if possible, |
| 105 | + # avoiding the need to compile, install and run it. |
| 106 | + # If that path does not exist, however, uses Glistix to build your |
| 107 | + # Gleam package from scratch instead, using the derivation |
| 108 | + # created further below (exported by this flake under |
| 109 | + # the 'packages' output). |
| 110 | + # |
| 111 | + # Specify 'forceLoadFromOutput = true;' above to opt into erroring |
| 112 | + # if the 'output/dev/nix' folder isn't found instead of invoking |
| 113 | + # the Glistix compiler. |
| 114 | + # |
| 115 | + # Pass 'system' to use the derivation for that system for compilation. |
| 116 | + # Pass 'glistix' to override the glistix derivation used for compilation. |
| 117 | + # Other arguments are passed through to Glistix's 'loadGlistixPackage'. |
| 118 | + # For example, 'lib.loadGlistixPackage { module = "ops/create"; }' |
| 119 | + # will load what's exported by your package's 'ops/create' module |
| 120 | + # as an attribute set. |
| 121 | + loadGlistixPackage = |
| 122 | + args@{ |
| 123 | + system ? builtins.currentSystem or null, |
| 124 | + glistix ? null, |
| 125 | + ... |
| 126 | + }: |
| 127 | + let |
| 128 | + derivation = |
| 129 | + if forceLoadFromOutput || system == null then |
| 130 | + null |
| 131 | + else if glistix != null then |
| 132 | + buildGlistixPackage { inherit system glistix; } |
| 133 | + else |
| 134 | + self.packages.${system}.default or null; |
| 135 | + |
| 136 | + overrides = builtins.removeAttrs args [ |
| 137 | + "system" |
| 138 | + "glistix" |
| 139 | + ]; |
| 140 | + loaderArgs = { |
| 141 | + inherit src derivation; |
| 142 | + output = outputPath; |
| 143 | + } // overrides; |
| 144 | + in |
| 145 | + glistixLib.loadGlistixPackage loaderArgs; |
| 146 | + |
| 147 | + in flake-parts.lib.mkFlake { inherit inputs; } { |
| 148 | + systems = import systems; |
| 149 | + |
| 150 | + flake = { lib = { inherit loadGlistixPackage; }; }; |
| 151 | + |
| 152 | + perSystem = { self', pkgs, lib, system, ... }: |
| 153 | + let |
| 154 | + # This derivation will build Glistix itself if needed |
| 155 | + # (using Rust), and then use Glistix to build this particular |
| 156 | + # package into Nix files. |
| 157 | + # The derivation's "out" output will contain the resulting |
| 158 | + # 'build' directory. You can use |
| 159 | + # "${derivation}/${derivation.glistixMain}" |
| 160 | + # for a path to this package's main '.nix' file, which can |
| 161 | + # be imported through Nix's `import`. |
| 162 | + package = buildGlistixPackage { inherit system; }; |
| 163 | + in { |
| 164 | + packages.default = package; |
| 165 | + |
| 166 | + # Run 'nix develop' to create a shell where 'glistix' is available. |
| 167 | + devShells.default = pkgs.mkShell { |
| 168 | + nativeBuildInputs = [ glistix.packages.${system}.default ]; |
| 169 | + }; |
| 170 | + }; |
| 171 | + }; |
| 172 | +} |
0 commit comments