diff --git a/.github/README.md b/README.md similarity index 77% rename from .github/README.md rename to README.md index bebf292..a64ee8f 100644 --- a/.github/README.md +++ b/README.md @@ -6,23 +6,23 @@ repository. ## Usage -To use a wallpaper in wallpkgs, you add the flake to your flake inputs. -You then may use each of the wallpapers however you please. +To use a wallpaper in wallpkgs, you add the flake to your flake inputs. You then +may use each of the wallpapers however you please. ```nix { - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - wallpkgs.url = "github:NotAShelf/wallpkgs"; - }; - - outputs = { wallpkgs, ... }: { - overlays.default = _final: prev: { - catppuccinWalls = prev.callPackage ./wallpapers.nix { - wallpapers = builtins.filter (wall: builtins.elem "catppuccin" wall.tags) (builtins.attrValues wallpkgs.wallpapers); - }; - }; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + wallpkgs.url = "github:NotAShelf/wallpkgs"; + }; + + outputs = {wallpkgs, ...}: { + overlays.default = _final: prev: { + catppuccinWalls = prev.callPackage ./wallpapers.nix { + wallpapers = builtins.filter (wall: builtins.elem "catppuccin" wall.tags) (builtins.attrValues wallpkgs.wallpapers); + }; }; + }; } ``` diff --git a/flake.nix b/flake.nix index b19773d..490530a 100644 --- a/flake.nix +++ b/flake.nix @@ -3,44 +3,17 @@ Pure and reproducible, and possibly curated collection of wallpapers. ''; - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - systems.url = "github:nix-systems/default"; - }; - - outputs = { - nixpkgs, - systems, - ... - }: let - inherit (nixpkgs) lib; + outputs = let + lib = import ./lib; + systems = ["x86_64-linux" "aarch64-linux"]; genSystems = lib.genAttrs (import systems); - pkgsFor = nixpkgs.legacyPackages; in { - wallpapers = let - fileExts = ["png" "jpg" "jpeg" "gif"]; - in - builtins.listToAttrs (map (n: let - findFile = builtins.filter builtins.pathExists (map (ext: ./wallpapers/${n}.${ext}) fileExts); - file = - if findFile == [] - then builtins.throw "Either ${n} isn't a file or it doesn't have the ${builtins.concatStringsSep ", " (lib.init fileExts)} or ${lib.last fileExts} extensions." - else builtins.head findFile; - in { - name = n; - value = { - path = file; - tags = lib.splitString "-" n; - hash = builtins.hashFile "sha256" file; - }; - }) - (map (n: builtins.head (lib.splitString "." n)) ( - builtins.attrNames ( - lib.filterAttrs (n: _: n != "README.md") (builtins.readDir ./wallpapers) - ) - ))); + # Construct the wallpapers output for wallpkgs from a directory and a list of + # extensions allow. + wallpapers = lib.toWallpkgs ./wallpapers ["png" "jpg" "jpeg" "gif"]; + # For backwards compatibility. This should re removed in the future. packages = genSystems (_: builtins.listToAttrs (map (n: { name = n; @@ -68,8 +41,5 @@ "monochrome" "nord" ])); - - # I do not accept anything else. - formatter = genSystems (system: pkgsFor.${system}.alejandra); }; } diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..a3b7dd3 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,78 @@ +# This is the wallpkgs... un-extended library. It appends a few useful functions +# based on their nixpkgs.lib variants, without any reliance on nixpkgs' own lib. +# Idea is that we do not need to pull an input for nixpkgs library alone, since +# wallpapers here are no longer packages. If implementing a new function, please +# remember to keep it lib-free. If it *must* rely on lib, then implement the +# function or functions you need as well as the main function. +let + splitString = separator: str: let + # "Nix doesn't have for loops it's a functional programming language!" + # You know what it has now? For loops. Nerd. + loop = str: acc: currentPos: currentSegment: + if currentPos == builtins.stringLength str + then + # end of string: add the current segment (even if it's empty) + acc ++ [currentSegment] + else let + char = builtins.substring currentPos 1 str; + in + if char == separator + then + # separator; add the current segment (even if it's empty) to acc + # and start a new segment for the next part + loop str (acc ++ [currentSegment]) (currentPos + 1) "" + else loop str acc (currentPos + 1) (currentSegment + char); + in + loop str [] 0 ""; + + filterAttrs = f: attrs: let + names = builtins.attrNames attrs; + filteredNames = builtins.filter (name: f name (attrs.${name})) names; + in + builtins.listToAttrs (map (name: { + name = name; + value = attrs.${name}; + }) + filteredNames); + + genAttrs = names: f: + builtins.listToAttrs (map (name: { + name = name; + value = f name; + }) + names); + + # TODO: This needs to be extensible, possibly in order to allow additional directories. + # In theory, we should only need to handle path*s* instead of a path, and search multiple + # paths by extension instead of just once, right? + toWallpkgs = path: extensions: let + fileExts = extensions; + in + builtins.listToAttrs (map (n: let + filesByExtension = builtins.filter builtins.pathExists (map (ext: ./${path}/${n}.${ext}) fileExts); + file = + if filesByExtension == [] + then builtins.throw "Either ${n} is not a file or it does not have the ${builtins.concatStringsSep ", " fileExts} extensions." + else builtins.head filesByExtension; + in { + name = n; + value = { + path = file; + tags = splitString "-" n; + hash = builtins.hashFile "md5" file; # in theory, md5 is the fastest option because it produces a 128-bit hash instead of >= 160 + }; + }) + (map (n: builtins.head (splitString "." n)) ( + builtins.attrNames ( + filterAttrs (n: _: n != "README.md") (builtins.readDir ./wallpapers) + ) + ))); +in { + # Partial re-implementations of functions from Nixpkgs. + # They may not work as intended, as the implementation is *completely* different. If this is + # the case while contributing, please create an issue! + inherit splitString filterAttrs genAttrs; + + # Main function to create the final collection of wallpapers from a given directory. + inherit toWallpkgs; +}