diff --git a/modules/features/example/default.nix b/modules/features/example/default.nix index 697fb2d..5b7935f 100644 --- a/modules/features/example/default.nix +++ b/modules/features/example/default.nix @@ -1,12 +1,25 @@ {inputs, ...}: { + # PerSystem, gives access to self' and inputs', as well as some helper functions perSystem = {pkgs, ...}: { + # Define packages as you would in a normal flake packages = { + # Most basic package definition, gives #example output that runs pkgs.hello + # All packages are outputs, and all outputs can be built, run and cached, etc. example = pkgs.hello; + # Universal wrapper, use this to quickly add flags to a program bash = inputs.wrappers.lib.wrapPackage { + # Always inherit pkgs inherit pkgs; + # Set package, could be anything package = pkgs.bash; + # Flag set, once again can be anything + # Nix doesn't know what flags are valid, it will accept any two valid nix objects + # Data types are irrelevant, can be paths, ints, bools, etc. flags = { "--rcfile" = ./bashrc; + # to add the same flag twice, make the right hand side a set + # "--someflag" = {"firstTime", "secondTime"} + # You cannot repeat the left hand side }; }; }; diff --git a/modules/features/zsh/default.nix b/modules/features/zsh/default.nix index 9dba3f7..08ac21d 100644 --- a/modules/features/zsh/default.nix +++ b/modules/features/zsh/default.nix @@ -1,50 +1,88 @@ { inputs, + # moduleWithSystem allows for using self' in modules moduleWithSystem, ... }: { + # enclose moduleWithSystem expression in (), so the final expression is one object flake.nixosModules.zsh = moduleWithSystem ({ - pkgs, - self', - ... - }: {config, ...}: let - lib = pkgs.lib; - in { - imports = []; - options = { - enableHighlighting = lib.mkOption { - type = lib.types.bool; - default = false; - description = "Enable syntax highlighting"; + pkgs, + # Self' allows accessing packages without having to point to the os string + self', + ... + }: + # This is a "double expression", being written like {...}: {...}: {}; + # having an extra {...}. This is because the first attribute set only contains + # flake parts attributes (self', flake.nixosModules, etc), and not nixos attributes. + # The usual nixos attributes, like config, are passed into a potential second attribute + # set, allowing you to access them there. This set is not implicit, and must be defined. + # This means that the code can be read as: + # {flake-parts attrs}: {nix attrs}: {actual expression}; + {config, ...}: + # A let in statement allows setting of variables, in this case pointing any reference + # of "lib" to "pkgs.lib". These can be stacked, and can be thought of as a way of + # scoping variables. + let + lib = pkgs.lib; + in { + # When the config or options objects are defined, nothing else can be in the expression. + # Options contains a list of defined options, and config becomes the wrapper for everything + # else in the expression. Imports works as expected. + imports = []; + options = { + # Make a new option with lib.mkOption + enableHighlighting = lib.mkOption { + # Set type, eg bool, str, int. Custom types can be created + type = lib.types.bool; + # Default value is required + default = false; + # Description is optional but useful + description = "Enable syntax highlighting"; + }; }; - }; - config = { - nixpkgs.overlays = [ - (final: prev: { - zsh = self'.packages.zsh; - }) - ]; - programs.zsh = { - enable = true; - enableSyntaxHighlighting = config.enableHighlighting; + config = { + # Nixpkgs overlay, replacing the universal "pkgs.zsh" with "self'.packages.zsh". + # As stated, self' allows you to shorten the package definition from + # self.${pkgs.stdenv.hostPlatform.system}.packages.zsh; + # into just self'.packages.zsh, meaning package platform errors are almost impossible. + nixpkgs.overlays = [ + (final: prev: { + zsh = self'.packages.zsh; + }) + ]; + programs.zsh = { + enable = true; + # You can use config options through the config attribute, in place of a normal value, + # the same way you can in other programming languages. + enableSyntaxHighlighting = config.enableHighlighting; + }; }; - }; - }); + }); + # PerSystem should be defined in the same scope as flake.nixosModules perSystem = { + # Inherited from inputs.nixpkgs pkgs, + # Custom arg, see modules/system/nix-settings.nix unstablePkgs, + # Same principle of self', removes need for direct reference to os-string inputs', + # Helper, points to pkgs.lib lib, ... }: { + # Defined wrapper for zsh, docs are on nix-wrapper modules site packages.zsh = inputs.wrappers.wrappers.zsh.wrap { + # Pkgs MUST be defined, always inherit pkgs in a wrapper inherit pkgs; zshAliases = { + # By making an alias reference a package, zsh becomes dependant on that package. + # This idea allows for fully portable modules with implicit dependancy trees some = lib.getExe unstablePkgs.vim; v = lib.getExe inputs'.nvim.packages.default; ff = lib.getExe pkgs.fastfetch; cat = lib.getExe pkgs.bat; }; + # Can also be zshrc.path, and path will take precedence over content. zshrc.content = '' export SOME=some ''; diff --git a/modules/hosts/newHost/newHost.nix b/modules/hosts/newHost/newHost.nix index d6083ca..6b71324 100644 --- a/modules/hosts/newHost/newHost.nix +++ b/modules/hosts/newHost/newHost.nix @@ -10,10 +10,15 @@ modules = with self.nixosModules; [ newHostConfiguration system - zsh - ({...}: { - enableHighlighting = true; - }) + zsh + + # Change config options in this object + # This can be done per-host with different options, + # allowing for smaller changes to modules' functionality + # Without having to create a whole new module + ({...}: { + enableHighlighting = true; + }) ]; }; } diff --git a/modules/system/nix-settings.nix b/modules/system/nix-settings.nix index a82feab..077facd 100644 --- a/modules/system/nix-settings.nix +++ b/modules/system/nix-settings.nix @@ -1,9 +1,23 @@ {inputs, ...}: { + # PerSystem, allows for universal module access perSystem = {system, ...}: { + # Pre-defined object _module.args = { + # Custom object, creating a new nixpkgs from a different input unstablePkgs = import inputs.nixpkgs-unstable { inherit system; }; + + # Another example not in the video, creating an unfree instance of nixpkgs + # Setting nixpkgs.config.free = true isn't possible through a normal nixosModule, + # because of how inheritance works with nixos exclusive objects + unfreePkgs = import inputs.nixpkgs { + inherit system; + config.allowUnfree = true; + }; + + # You can also set universal variables here, if you need to access the same info across files + someVariable = "import this as you would import pkgs, in a perSystem block"; }; }; }