diff --git a/README.md b/README.md index af49996..397b14c 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,10 @@ This project is configured by module [project.nix](./project.nix) # create .envrc for direnv files.direnv.enable = true; + + # disabe file creation when entering in the shell + # call devshell-files instead + # files.on-call = true; } ``` @@ -602,6 +606,7 @@ To document our modules is simple, we just need to use `config.files.docs` as fo files.docs."/gh-pages/src/modules/cmds.md".modules = [ ../modules/cmds.nix ]; files.docs."/gh-pages/src/modules/files.md".modules = [ ../modules/files.nix ]; files.docs."/gh-pages/src/modules/git.md".modules = [ ../modules/git.nix ]; + files.docs."/gh-pages/src/modules/on-call.md".modules = [ ../modules/startup.nix ]; files.docs."/gh-pages/src/modules/gitignore.md".modules = [ ../modules/gitignore.nix ]; files.docs."/gh-pages/src/modules/hcl.md".modules = [ ../modules/hcl.nix ]; files.docs."/gh-pages/src/modules/json.md".modules = [ ../modules/json.nix ]; @@ -697,6 +702,7 @@ They are already included when we use this package. - `files.cmds`, install packages from [nix repository](https://search.nixos.org/) - `files.docs`, convert our modules file into markdown using [nmd](https://gitlab.com/rycee/nmd) - `files.git`, configure git with file creation +- `files.on-call`, connfigure file to created only when devshell-files command is called, not on shell start - `files.gitignore`, copy .gitignore from [templates](https://github.com/github/gitignore/) - `files.hcl`, create HCL files with nix syntax - `files.json`, create JSON files with nix syntax diff --git a/examples/docs.nix b/examples/docs.nix index 4b3905b..818401f 100644 --- a/examples/docs.nix +++ b/examples/docs.nix @@ -4,6 +4,7 @@ files.docs."/gh-pages/src/modules/cmds.md".modules = [ ../modules/cmds.nix ]; files.docs."/gh-pages/src/modules/files.md".modules = [ ../modules/files.nix ]; files.docs."/gh-pages/src/modules/git.md".modules = [ ../modules/git.nix ]; + files.docs."/gh-pages/src/modules/on-call.md".modules = [ ../modules/startup.nix ]; files.docs."/gh-pages/src/modules/gitignore.md".modules = [ ../modules/gitignore.nix ]; files.docs."/gh-pages/src/modules/hcl.md".modules = [ ../modules/hcl.nix ]; files.docs."/gh-pages/src/modules/json.md".modules = [ ../modules/json.nix ]; diff --git a/examples/readme/modules/builtins.md b/examples/readme/modules/builtins.md index 9ad0436..df2fb3c 100644 --- a/examples/readme/modules/builtins.md +++ b/examples/readme/modules/builtins.md @@ -8,6 +8,7 @@ They are already included when we use this package. - `files.cmds`, install packages from [nix repository](https://search.nixos.org/) - `files.docs`, convert our modules file into markdown using [nmd](https://gitlab.com/rycee/nmd) - `files.git`, configure git with file creation +- `files.on-call`, connfigure file to created only when devshell-files command is called, not on shell start - `files.gitignore`, copy .gitignore from [templates](https://github.com/github/gitignore/) - `files.hcl`, create HCL files with nix syntax - `files.json`, create JSON files with nix syntax diff --git a/flake.nix b/flake.nix index a8a3964..511d964 100644 --- a/flake.nix +++ b/flake.nix @@ -25,6 +25,7 @@ ./modules/toml.nix ./modules/yaml.nix ./modules/hcl.nix + ./modules/startup.nix ./modules/git.nix ./modules/gitignore.nix ./modules/spdx.nix diff --git a/modules/dockerfile.nix b/modules/dockerfile.nix index 406206e..4a4d5e4 100644 --- a/modules/dockerfile.nix +++ b/modules/dockerfile.nix @@ -3,11 +3,12 @@ let cfg = config.files.dockerfile; # directives = pkgs.callPackage ./dockerfile/directives.nix { inherit lib; }; toFile = name: value: { - source = pkgs.writeTextFile { + source = pkgs.writeTextFile { name = (builtins.baseNameOf name); text = value; }; - git-add = lib.mkIf config.files.git.auto-add true; + git-add = lib.mkIf config.files.git.auto-add true; + on-enter = lib.mkIf config.files.on-call (lib.mkDefault false); }; in { options.files.dockerfile = lib.mkOption { diff --git a/modules/file-format.nix b/modules/file-format.nix index 532ca2e..4857705 100644 --- a/modules/file-format.nix +++ b/modules/file-format.nix @@ -13,8 +13,9 @@ let value = builtins.toJSON value; } ''yj ${yj-arg} < $valuePath > $out''; toFile = name: value: { - source = gen name value; - git-add = lib.mkIf config.files.git.auto-add true; + source = gen name value; + git-add = lib.mkIf config.files.git.auto-add (lib.mkDefault true); + on-enter = lib.mkIf config.files.on-call (lib.mkDefault false); }; in { options.files.${format} = lib.mkOption { diff --git a/modules/file-type.nix b/modules/file-type.nix index 9f974b9..b81599c 100644 --- a/modules/file-type.nix +++ b/modules/file-type.nix @@ -59,6 +59,14 @@ in To add this file to git repository after creation ''; }; + + on-enter = mkOption { + type = types.nullOr types.bool; + default = true; + description = '' + This file will be created on enter in development shell + ''; + }; }; config = { diff --git a/modules/files.nix b/modules/files.nix index 69799f0..91b36bd 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -16,23 +16,23 @@ let else ""; toName = name: ".dsf${lib.strings.sanitizeDerivationName name}"; # Execute this script to update the project's files - copy-file = name: file: pkgs.writeShellScriptBin "${toName name}" '' - target="$PRJ_ROOT${file.target}" - ${pkgs.coreutils}/bin/install -m 644 -D ${file.source} $target + copy-files = map (name: "source $DEVSHELL_DIR/bin/${toName name}") (builtins.attrNames files); + copy-files'= lib.mapAttrsToList (name: file: if file.on-enter then "source $DEVSHELL_DIR/bin/${toName name}" else "") files; + copy-file = name: file: pkgs.writeShellScriptBin "${toName name}" '' + ${pkgs.coreutils}/bin/install -m 644 -D ${file.source} "$PRJ_ROOT${file.target}" ${chmod file} ${git-add file} ''; - cmd.command = builtins.concatStringsSep "\n" startups; + cmd.command = builtins.concatStringsSep "\n" copy-files; cmd.help = "Recreate files"; cmd.name = "devshell-files"; opt.default = {}; opt.description = "Attribute set of files to create into the project root."; opt.type = fileType "PRJ_ROOT"; - startup.devshell-files.text = "$DEVSHELL_DIR/bin/devshell-files"; - startups = map (name: "source $DEVSHELL_DIR/bin/${toName name}") (builtins.attrNames files); + startup.devshell-files.text = builtins.concatStringsSep "\n" copy-files'; in { options.file = lib.mkOption opt; - config.commands = lib.mkIf (builtins.length startups > 0) [ cmd ]; + config.commands = lib.mkIf (builtins.length copy-files > 0) [ cmd ]; config.devshell.packages = lib.mapAttrsToList copy-file files; - config.devshell.startup = lib.mkIf (builtins.length startups > 0) startup; + config.devshell.startup = lib.mkIf (builtins.length copy-files > 0) startup; } diff --git a/modules/git.nix b/modules/git.nix index c8df5fb..74a036a 100644 --- a/modules/git.nix +++ b/modules/git.nix @@ -1,6 +1,4 @@ {pkgs, config, lib, ...}: -let - cfg = config.files.git; -in { +{ options.files.git.auto-add = lib.mkEnableOption "auto add files to git after creation"; } diff --git a/modules/startup.nix b/modules/startup.nix new file mode 100644 index 0000000..7fd5104 --- /dev/null +++ b/modules/startup.nix @@ -0,0 +1,4 @@ +{pkgs, config, lib, ...}: +{ + options.files.on-call = lib.mkEnableOption "Files will be created when devshell-files command is called instead of when start the shell"; +} diff --git a/modules/text.nix b/modules/text.nix index 3620075..6751ce3 100644 --- a/modules/text.nix +++ b/modules/text.nix @@ -3,8 +3,9 @@ let format = "text"; cfg = config.files.${format}; toFile = name: value: { - git-add = lib.mkIf config.files.git.auto-add true; - source = pkgs.writeTextFile { + git-add = lib.mkIf config.files.git.auto-add true; + on-enter = lib.mkIf config.files.on-call (lib.mkDefault false); + source = pkgs.writeTextFile { name = (builtins.baseNameOf name); text = value; }; diff --git a/project.nix b/project.nix index 5c3afe7..72b4f88 100644 --- a/project.nix +++ b/project.nix @@ -50,4 +50,8 @@ # create .envrc for direnv files.direnv.enable = true; + + # disabe file creation when entering in the shell + # call devshell-files instead + # files.on-call = true; }