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

Function to get file size #7466

Open
shyim opened this issue Dec 15, 2022 · 7 comments
Open

Function to get file size #7466

shyim opened this issue Dec 15, 2022 · 7 comments
Labels
feature Feature request or proposal

Comments

@shyim
Copy link
Member

shyim commented Dec 15, 2022

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

Currently, we are able to hash a file and read a file, but we can't get the file size

Describe the solution you'd like
I would like to have a function to get the file size of the given path

Describe alternatives you've considered

lib.toInt (lib.fileContents (pkgs.runCommand "update.zip" { } ''
              du -b ${./update.zip} | cut -f1 > $out;
            ''))

Additional context
Add any other context or screenshots about the feature request here.

Priorities

Add 👍 to issues you find important.

@shyim shyim added the feature Feature request or proposal label Dec 15, 2022
@Ericson2314
Copy link
Member

Ericson2314 commented Dec 15, 2022

CC @roberth @aakropotkin as we were talking about this in #7447

@infinisil
Copy link
Member

@shyim What are the use cases for such a function?

@shyim
Copy link
Member Author

shyim commented Dec 23, 2022

I generate a JSON for Wiremock (API mocking) in Nix and to mock an update service I need the sha1, sha256 of that file and the fileSize. Sha is already possible and didn't found a proper way to get the fileSize so I came to this issue :)

@infinisil
Copy link
Member

Hmm, this doesn't sound very convincing, it's not really a use case Nix is meant for, and I feel like there's many ways to achieve the same result without Nix.

@aakropotkin
Copy link
Contributor

If it were possible to lock inputs with fileSize I would agree with this being useful; but because filesize says nothing about the contents you can't lock, and if you can't lock what's the point?

If you really want filesize as a built in it's a great opportunity to write a small plugin though.

@shyim
Copy link
Member Author

shyim commented Dec 24, 2022

with plugin you mean a PR here? 🤔

@roberth
Copy link
Member

roberth commented Dec 24, 2022

If it were possible to lock inputs with fileSize I would agree with this being useful

Adding the file size to lock files (or FOD hashes!) is nice for giving an indication of total download size. There's a case to be made for that, but locking is not implemented in the Nix language, so I don't see that as a use case for a file size builtin.

Of course fileSize is derived info that is merely convenient to store in a lock file, and not essential.

I would like to have a function to get the file size of the given path

This sounds very hypothetical

I generate a JSON for Wiremock (API mocking) in Nix

Now we're talking.
This mock service doesn't run in Nix, so presumably this information can be computed in the derivation that writes the mocking config? That will also perform slightly better. The less work we do in the Nix language, the faster evaluation will be, and derivations are by nature cached.

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

No branches or pull requests

5 participants