Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symlink the configuration file to the root of the project #329

Open
teamwolfyta opened this issue Mar 30, 2025 · 2 comments
Open

Symlink the configuration file to the root of the project #329

teamwolfyta opened this issue Mar 30, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@teamwolfyta
Copy link

teamwolfyta commented Mar 30, 2025

Is your feature request related to a problem? Please describe.

  • treefmt-vscode won't function because it can't find either of these files: treefmt.toml .treefmt.toml.

Describe the solution you'd like

  • You could you perhaps do what git-hooks does and symlink the generated config to the root of the project. How will be up for debate.

Describe alternatives you've considered

  • Manually symlinking the file in my devShell but obviously this will get repetitive.
@teamwolfyta teamwolfyta added the enhancement New feature or request label Mar 30, 2025
@teamwolfyta
Copy link
Author

teamwolfyta commented Mar 30, 2025

After researching a bit, i discovered git-hooks uses git to first figure out the root of the project, check for an existing .pre-commit-config.yaml, if there is one it and it's not a symlink it will error out else it will create or update the symlink from what i presume is the config file created in the nix store to the config file in the root of your project.

  • Get's the root of the project here using git.
  • Update's the existing symlink config file here.
  • Check's if there's an existing config file here.
  • And finally symlinks the file here.

There's some extra code but not sure what that's for, the code below that last hyperlink is specific to pre-commit.

I'm not sure this project would appreciate doing a similar implementation as it then requires a git repository to be initiated to use treefmt-nix.

@teamwolfyta
Copy link
Author

teamwolfyta commented Mar 30, 2025

For anyone else who's searching for a solution, This is what I've temporarily done to my devShell:

    shellHook = ''
      FLAKE_ROOT=$(${getExe gitMinimal} rev-parse --show-toplevel)
      SYMLINK_SOURCE_PATH="${treefmt.build.configFile}"
      SYMLINK_TARGET_PATH="$FLAKE_ROOT/.treefmt.toml"

      if [[ -e "$SYMLINK_TARGET_PATH" && ! -L "$SYMLINK_TARGET_PATH" ]]; then
        echo "treefmt-nix: Error: Target exists but is not a symlink."
        exit 1
      fi

      if [[ -L "$SYMLINK_TARGET_PATH" ]]; then
        if [[ "$(readlink "$SYMLINK_TARGET_PATH")" != "$SYMLINK_SOURCE_PATH" ]]; then
          echo "treefmt-nix: Removing existing symlink"
          unlink "$SYMLINK_TARGET_PATH"
        else
          exit 0
        fi
      fi

      nix-store --add-root "$SYMLINK_TARGET_PATH" --indirect --realise "$SYMLINK_SOURCE_PATH"
      echo "treefmt-nix: Created symlink successfully"
    '';

I use flake-parts with the treefmt-nix flakeModule. treefmt in this code is just config.treefmt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant